Java thread exception processor, Java thread processor

Source: Internet
Author: User

Java thread exception processor, Java thread processor

Thread. UncaughtExceptionHandler is a static internal interface of the Thread class. This interface has only one method:

Void uncaughtException (Thread t, Throwable e), where t represents the Thread with an exception and the exception thrown by the e-table.

If an unhandled exception is thrown during thread execution, the corresponding unhandled exception processor is found before the thread ends, and the preceding method is called to handle the exception.

The Thread class provides two methods to set the exception processor.

Static setDefaultUncaughtExceptionHandler (Thread. UncaughtExceptionHandler eh) sets the default exception processor for all objects in the Thread class.

SetUncaughtExceptionHandler (Thread. UncaughtExceptionHandler eh) sets an exception processor for the specified Thread object.

Example:

Public class UnCaughtExceptionHandlerTest {static class UnCaughtExceptionHandler implements Thread. uncaughtExceptionHandler {@ Override public void uncaughtException (Thread t, Throwable e) {System. out. println (t + "exception:" + e) ;}} public static void main (String [] args) {Thread. currentThread (). setUncaughtExceptionHandler (new UnCaughtExceptionHandler (); int a = 5/0; System. out. println ("normal ove R! ");}}

Execution result

Thread [main, 5, main] exception: java. lang. ArithmeticException:/by zero

It can be seen from the above that, unlike catch, after the exception processor handles the exception, the program will not continue to run, so it will not end normally.

Related Article

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.