Java thread exception processor, Java thread processor
Thread. UncaughtExceptionHandler is a static internal interface of the Thread class. This interface has only one method:
Void uncaughtException (Thread t, Throwable e), where t represents the Thread with an exception and the exception thrown by the e-table.
If an unhandled exception is thrown during thread execution, the corresponding unhandled exception processor is found before the thread ends, and the preceding method is called to handle the exception.
The Thread class provides two methods to set the exception processor.
Static setDefaultUncaughtExceptionHandler (Thread. UncaughtExceptionHandler eh) sets the default exception processor for all objects in the Thread class.
SetUncaughtExceptionHandler (Thread. UncaughtExceptionHandler eh) sets an exception processor for the specified Thread object.
Example:
Public class UnCaughtExceptionHandlerTest {static class UnCaughtExceptionHandler implements Thread. uncaughtExceptionHandler {@ Override public void uncaughtException (Thread t, Throwable e) {System. out. println (t + "exception:" + e) ;}} public static void main (String [] args) {Thread. currentThread (). setUncaughtExceptionHandler (new UnCaughtExceptionHandler (); int a = 5/0; System. out. println ("normal ove R! ");}}
Execution result
Thread [main, 5, main] exception: java. lang. ArithmeticException:/by zero
It can be seen from the above that, unlike catch, after the exception processor handles the exception, the program will not continue to run, so it will not end normally.