The function and simple usage of UncaughtExceptionHandler

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.UncaughtExceptionHandlerthe 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.

image-20201218225951286

So when an exception is encountered in the thread, we try-catchcan only catch the exception as much as possible.

image-20201218225951286

But in the run method of each thread, it is impossible for us to wrap every sentence of code in a large try-catchcode 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/0error, which is ArithmeticExceptionan exception. This is an unchecked exception, so we did not try-catchwrap 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.
insert image description here
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.
insert image description here
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 runin the method , then the program will terminate because of the exception?try- catch

Of course not, at this time you can make UncaughtExceptionHandleryour 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.UncaughtExceptionHandlerthe interface.

So, I MyUncaughtExceptionHandlerrewrote the method in the class Thread.UncaughtExceptionHandler, uncaughtExceptionand defined the handling of exceptions in this method. Then thread1 startbefore, use setUncaughtExceptionHandlerthe 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.
insert image description here

Reprinted: https://blog.csdn.net/qq_42799615/article/details/111658473

Guess you like

Origin blog.csdn.net/gqg_guan/article/details/132326136