Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run the java thread in UncaughtExceptionHandler?

Is it possible to recover java thread by doing the next?

Thread.setDefaultExceptionHandler(new UncaughtExceptionHandler() {
    public void unchaughtException(Thread t, Throwable e) {
        t.start();
    }
});
like image 578
Rubycon Avatar asked Feb 04 '26 05:02

Rubycon


2 Answers

Yes it is possible to run a Thread in a Thread.UncaughtExceptionHandler.uncaughtException ... provided that the Thread hasn't been started previously.

But it is NOT possible to start the Thread that was pass as the t argument. That will (always) be a Thread that has already been started and has terminated.

You can start a given Thread at most once. If you try to start one a second time you will get an InvalidStateException. Always.

like image 190
Stephen C Avatar answered Feb 06 '26 17:02

Stephen C


No, you cannot run the thread that threw the exception, as shown in your code. It has already run. That's how it threw the exception. A thread cannot be started more than once.

like image 20
user207421 Avatar answered Feb 06 '26 17:02

user207421