Is it possible to start java thread in UncaughtExceptionHandler?
Is it possible to restore a java stream by doing the following?
Thread.setDefaultExceptionHandler(new UncaughtExceptionHandler() {
public void unchaughtException(Thread t, Throwable e) {
t.start();
}
});
Yes, you can run Thread
in Thread.UncaughtExceptionHandler.uncaughtException
... provided that it has Thread
not been launched before.
But it is not possible to run Thread
that was passed as an argument t
. It will (always) be Thread
, which has already been started and completed.
You can run the specified Thread
maximum once. If you try to run a second time, you get InvalidStateException
. Is always.
No, you cannot start the thread that threw the exception, as shown in your code. It has already been launched. This is like an exception. A thread cannot be started more than once.