Analysis of Exception Handling Mechanism in java Multithreading

Source: Internet
Author: User

In a java multi-threaded program, all threads are not allowed to throw uncaptured checked exception. That is to say, each thread needs to handle its own checked exception. This is a constraint through the java. lang. Runnable. run () method declaration (because the throw exception section is not declared on this method. However, the thread may still throw an unchecked exception. When such an exception is thrown, the thread ends, and the main thread and other threads are not affected at all, the exception thrown by a thread is completely invisible (that is, the exception cannot be caught at all ). This design of JVM comes from the idea that "a thread is a piece of code that is executed independently. The thread problem should be solved by the thread rather than being delegated to the outside ." Based on this design concept, in Java, the exceptions of the thread method (whether checked or unchecked exception) should be within the thread code boundary (within the run method) try catch and process it.

But if the thread does not try to catch an unchecked exception, and we want to catch and handle this exception outside the thread code boundary (outside the run method, java provides a callback mechanism for handling exceptions beyond the boundary of the Thread code when exceptions occur in the Thread, that is, the setUncaughtExceptionHandler (Thread. uncaughtExceptionHandler eh) method.

By setting an UncaughtExceptionHandler for a thread, you can call back the public void uncaughtException (Thread t, Throwable e) method of the UncaughtExceptionHandler interface to handle the exception when the thread encounters an exception, this advantage or purpose is to be able to handle non-captured exceptions outside the Thread code boundary (beyond the Thread's run () method. However, it should be particularly clear that although the exception is handled in the callback method, this callback method is still in the thread where the exception is thrown during execution!

Compared with the above method, there is also a programming processing method that can be used for reference, that is, sometimes the caller of the main thread may just want to know what exceptions have occurred during the Sub-thread execution, instead, it may not be processed or processed immediately. Then, the method for sending the sub-thread can collect the Exception instance thrown by the sub-thread and return it to the caller as a List of exceptions, the caller determines how to handle exceptions. Note that the subthread is terminated at this time.

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.