Analysis of java thread group and unhandled exception instances

This article provides examples of java thread groups and unhandled exceptions. Share with you for your reference, as follows:

A finishing touch

From jdk1 Starting from 5, Java has strengthened the exception handling of threads. If an unhandled exception is thrown during thread execution, the JVM will automatically find out whether there is a corresponding thread before ending the thread Uncaughtexceptionhandler object. If the processor object is found, the uncaughtexception (thread T, throwable E) method of the object will be called to handle the exception.

Thread. Uncaughtexceptionhandler is an internal public static interface of thread class. There is only one method in this interface: void uncaughtexception (thread T, throwable E). T in this method represents the thread with exception, and e represents the exception thrown by this thread.

Default process for thread group exception handling:

1 if the thread group has a parent thread group, call the uncaughtexception method of the parent thread group to handle the exception.

2 if the thread class to which the thread instance belongs has a default exception handler (the exception handler set by the setdefaultuncaughtexceptionhandler method), call the exception handler to handle the exception.

3. If the exception object is a ThreadDeath object, no processing will be performed; Otherwise, the information of the exception tracking stack will be printed to system Err error output stream and end the thread.

II. Actual combat

1 code

// 定义自己的异常处理器
class MyExHandler implements Thread.UncaughtExceptionHandler
{
   // 实现uncaughtException方法,该方法将处理线程的未处理异常
   public void uncaughtException(Thread t,Throwable e)
   {
      System.out.println(t + " 线程出现了异常:" + e);
   }
}
public class ExHandler
{
   public static void main(String[] args)
   {
      // 设置主线程的异常处理器
      Thread.currentThread().setUncaughtExceptionHandler
        (new MyExHandler());
      int a = 5 / 0;   // ①
      System.out.println("程序正常结束!");
   }
}

2 operation

3 description

The exception handler specified by the program handled the uncapped exception, but the program will not end normally. This shows that the exception handler is different from catching exceptions through catch - when using catch to catch exceptions, exceptions will not be passed up to the upper level caller, but after using the exception handler to handle exceptions, exceptions will still be passed to the upper level caller.

For more Java related content, interested readers can view the special topics of this site: summary of java process and thread operation skills, tutorial on Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>