UncaughtExceptionHandler
The thread's run method cannot throw any checked exceptions, but unchecked exceptions may cause the thread to terminate. In this case, the thread will die, which will also cause the main thread to terminate abnormally.。
For exceptions that can be propagated, the exception is passed to a handler for uncaught exceptions before the thread dies. The premise is that the processor must be a class that implements Thread.UncaughtExceptionHandler
the interface。
What do these words mean? Read on.
As follows, if I forcibly throw an exception in the run method, an error will be reported.
So when an exception is encountered in the thread, we try-catch
can only catch the exception as much as possible.
But in the run method of each thread, it is impossible for us to wrap every sentence of code in a large try-catch
code block. Checked exceptions have to be wrapped with try-catch code blocks, but there are also many unchecked exceptionstry-catch
. We cannot guarantee that unchecked exceptions will not go wrong, but it is not appropriate to wrap all unchecked exceptions .
Please see the code below.
The first piece of code:
package concurrence;
public class UncaughtExceptionHandlerTest {
public static void main(String[] args) throws InterruptedException {
try {
Thread thread1 = new Thread(new MyRunnable());
thread1.start();
} catch (Exception ex) {
System.out.println("Exception:" + ex.getMessage());
}
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println(2 / 1);
System.out.println(2 / 0);
System.out.println(2 / 2);
}
}
In the run method of thread1, there is an 2/0
error, which is ArithmeticException
an exception. This is an unchecked exception, so we did not try-catch
wrap it with a code block. But once the code throws an unchecked exception , we cannot catch it in other threads. Therefore, although I tried to catch unchecked exceptions in the main thread , I failed to catch them, and eventually the program had to terminate due to exceptions.
Normally, see:
The second piece of code:
package concurrence;
public class Test {
public static void main(String[] args) {
try {
dividByZero();
} catch (Exception ex) {
System.out.println("Exception:" + ex);
}
}
public static void dividByZero() {
System.out.println(2 / 1);
System.out.println(2 / 0);
System.out.println(2 / 2);
}
}
Under normal circumstances, we can catch unchecked exceptions so that our program will not terminate due to exceptions.
But now, in multithreading, we cannot catch unchecked exception in run method . Does this mean that if we don't catch the unchecked exception with a code block run
in the method , then the program will terminate because of the exception?try- catch
Of course not, at this time you can make UncaughtExceptionHandler
your debut.
For exceptions that can be propagated, the exception is passed to a handler for uncaught exceptions before the thread dies. The premise is that the processor must be a class that implements Thread.UncaughtExceptionHandler
the interface.
So, I MyUncaughtExceptionHandler
rewrote the method in the class Thread.UncaughtExceptionHandler
, uncaughtException
and defined the handling of exceptions in this method. Then thread1
start
before, use setUncaughtExceptionHandler
the method to set the thread.
The third piece of code:
package concurrence;
public class UncaughtExceptionHandlerTest {
public static void main(String[] args) throws InterruptedException {
try {
Thread thread1 = new Thread(new MyRunnable());
thread1.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
thread1.start();
} catch (Exception ex) {
System.out.println("Exception:" + ex.getMessage());
}
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println(2 / 1);
System.out.println(2 / 0);
System.out.println(2 / 2);
}
}
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t.getName() + " " + e.getMessage());
}
}
In this way, when a non-checked exception occurs in the run method , before the death of thread1, we can catch the exception in the main thread, so that the main program can also end normally, and it will not be terminated because of the exception.
Reprinted: https://blog.csdn.net/qq_42799615/article/details/111658473