Unchecked Exception Handling in Java multithreading uncaughtexceptionhandler

Source: Internet
Author: User

The thread run method does not throw any checked exception, but it may be terminated due to an exception, resulting in the end of the thread. The most troublesome thing is that exceptions thrown in the thread even if try is used in the main thread... catch cannot be intercepted, so some problems may occur. For example, some system resources cannot be recycled during an exception, or the current connection is not closed.


The main thread does not handle the runtimeexception thrown by the sub-thread because the thread is asynchronous and the sub-thread is not finished. The main thread may have ended.


The uncaughtexceptionhandler name means to handle uncaptured exceptions. More specifically, it handles uncaptured runtime exceptions. The Java compiler must handle all non-runtime exceptions. Otherwise, the program cannot be compiled. Here, "processing" refers to the exception declared by the throws clause in the method or the exception of the catch clause in the try-Catch Block.

Package demo; import Java. lang. thread. uncaughtexceptionhandler; public class threadtest {public static void main (string [] ARGs) {threada A = NULL; try {errhandler handle = new errhandler (); A = new threada (); a. setuncaughtexceptionhandler (handle); // Add the defined errhandler. start (); // The runtimeexception thrown by the run thread cannot be caught. //. run (); catch (exception e) {system. out. println ("catch runtimeexception a"); // does not work, but the exception has been handed over to handle for processing} // normal threads even if try... catch cannot catch the thrown exception. Try {threadb B = new threadb (); B. start ();} catch (exception e) {system. out. println ("catch runtimeexception B "); // does not work}/*** a custom uncaughtexceptionhandler */class errhandler implements uncaughtexceptionhandler {/*** can handle any exceptions here, for example, logging, etc. */Public void uncaughtexception (thread a, throwable e) {system. out. println ("this is:" +. getname () + ", message:" + E. getmessage (); E. printstacktrace () ;}}/*** threads with uncaughtexceptionhandler */class threada extends thread {public threada () {} public void run () {double I = 12/0; // runtimeexception type exception that throws arithmeticexception}/*** common thread */class threadb extends thread {public threadb () {} public void run () {try {double I = 12/0; // runtimeexception type exception that throws arithmeticexception} catch (runtimeexception e) {Throw e ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.