In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "Java thread group and unhandled exception case analysis". The content of the article is simple and clear, and it is easy to learn and understand. let's study and learn "Java thread group and unhandled exception case analysis".
A little bit of eye contact
Starting from JDK1.5, Java strengthens the exception handling of a thread. If an unhandled exception is thrown during thread execution, JVM will automatically find out whether there is a corresponding Thread.UncaughtExceptionHandler object before ending the thread. 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 the Thread class, in which there is only one method: void uncaughtException (Thread t, Throwable e), where t represents the thread that has an exception, and e represents the exception thrown by that thread.
The default process for thread groups to handle exceptions:
1 if the thread group has a parent thread group, the uncaughtException method of the parent thread group is called to handle the exception.
2 if the thread instance belongs to a thread class that has a default exception handler (the exception handler set by the setDefaultUncaughtExceptionHandler method), the exception handler is called to handle the exception.
3 if the exception object is an object of ThreadDeath, no processing will be done; otherwise, the information of the exception trace stack will be printed to the System.err error output stream and the thread will be terminated.
Second actual combat
1 code
/ define your own exception handler class MyExHandler implements Thread.UncaughtExceptionHandler {/ / implement the uncaughtException method, which handles the thread's unhandled exception public void uncaughtException (Thread t, Throwable e) {System.out.println (t + "thread has an exception:" + e);}} public class ExHandler {public static void main (String [] args) {/ / sets the main thread's exception handler Thread.currentThread () .setUncaughtExceptionHandler (new MyExHandler ()) Int a = 5 / 0; / / ① System.out.println ("Program ends normally!") ;}}
2 run
The Thread [main,5,main] thread encountered an exception: java.lang.ArithmeticException: / by zero
3 description
The exception handler specified by the program handles the uncaught exception, but the program still does not end normally. This shows that an exception handler is different from catching an exception through catch-when an exception is caught using catch, the exception is not passed up to the upper-level caller, but after the exception is handled using the exception handler, the exception is still passed to the upper-level caller.
Thank you for your reading, the above is the content of "Java thread group and unhandled exception instance analysis". After the study of this article, I believe you have a deeper understanding of the problem of Java thread group and unhandled exception case analysis. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.