Run Function on Exception or Throwable Without try{} block

Here is problem I have main game thread and using JFrame
JFrame on setVisible create not daemon Thread AWT-EventQueue
So when my main process Thread do all work,
and I don’t have other no deamon thread Application still running
To finish it need or hide JFrame or use System.exit

The problem in error Exception,
The standard way is surround main function
try{}catch or finally
But I need debug in Eclipse start Exception and try{} block skip it.
For fix error in debug mode.

So we need make some action on Exception not using try{}

Here is my solution
Closer Thread something similar like base JVM Check for no daemon


public class Closer_Thread extends Thread{
	public Closer_Thread(){
		super();
		setDaemon(true);
		setName("Closer");
		setPriority(Thread.MIN_PRIORITY);
	}
	@Override
	public void run(){
		while(true){		
			try{
				sleep(1000);// every sec
			}catch (InterruptedException e){
				e.printStackTrace();
			}
//Start.Game_Thread my main Procces Thread 
			if(!Start.Game_Thread.isAlive()){
/*finalize_Ap(){
etc(); // my case stop server client - send exit packets
System.Exit(0);
}*/
					Start.finalize_Ap();
			}
		}
	}
}

I have no idea what your question is, or what your problem is, but I’m guessing that you forgot to do

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

on your JFrame. That’ll close the app, once the window closes.

Not really what I am talking about :wink:

And what if You have custom Process Thread that handle player actions etc…
And game in fullscrean mode or setUndecorated(true);
you simple don’t have “X” button to close window
and all game action stopped because Main Process Thread fishised
so you cant exit simple way only Use try{} block,
to catch Exception in Thread and then finish Process.

Update:
In my case I Have 2 Threads: 1 Procces(not daemon) and 2 Client(daemon),
so you think if main thread(Procces) stops - Application will be closed,
But no - Thread AWT-EventQueue don’t give you close Application.
I don’t find way to make Thread AWT-EventQueue Daemon.

And I cant use try{} because I need debug problem,
That’s why I create “Close” Thread ^^

p.s it is difficult to understand this problem until you meet the problem themselves :wink:

You got exception. What is it?