Java Thread Learning Note (two) thread exception handling

Source: Internet
Author: User

Thread catch Exception:

Case, we are in the main () method is not catching the exception of the thread, than for example with the following code:

public class Exceptionthread implements runnable{    @Override public    void Run () {        throw new NullPointerException ();    }    public static void Main (string[] args) {        Executorservice executorservice = Executors.newcachedthreadpool ();        try {            System.out.println ("Running thread");            Executorservice.execute (New Exceptionthread ());        } catch (Exception e) {            e.printstacktrace ();            System.out.println ("catch Exception");}}}    
The above code does not catch thread exceptions in the main method, so how can we capture the thread's exception information?

Let's look at the code below

/** * Define exception thread content */class Myexceptionthread implements Thread.uncaughtexceptionhandler {@Override public void uncaught        Exception (Thread T, Throwable e) {///catch exception after the business processing is placed here System.out.println ("Catch exception information such as the following");    System.out.println (e); }}/** * Define exception Thread factory */class Exceptionthreadfactory implements threadfactory {@Override public thread newthread (runnabl        E r) {thread thread = new Thread (r);        Here is the setting to catch the exception.        Thread.setuncaughtexceptionhandler (New Myexceptionthread ());    return thread; }}/** * Execution Thread */class ExceptionThread2 implements runnable{@Override public void Run () {try {Thr        Ead.sleep (1000l);        } catch (Interruptedexception e) {e.printstacktrace ();    }//Throw exception throw new NullPointerException (); } public static void Main (string[] args) {//through our own exceptionthreadfactory thread factory, construct thread pool Executorservice ex Ecutorservice = Executors.newcachedthreadpool (New ExceptiOnthreadfactory ());            try {System.out.println ("execution thread");            Start three threads Executorservice.execute (new ExceptionThread2 ());            Executorservice.execute (New ExceptionThread2 ());        Executorservice.execute (New ExceptionThread2 ());            } catch (Exception e) {e.printstacktrace ();        System.out.println ("catch exception"); }    }}
The result of the above output is
The exception information that is captured by running threads such as the following java.lang.NullPointerException catch exception information such as the following java.lang.NullPointerException catch exception information such as the following JAVA.LANG.NULLPOINTEREXC Eption
The conclusion is: Main () The method still does not catch the exception of the thread, of course, this setting is reasonable. Now each thread has its own exception handling mechanism, how to do it, sentence, when creating a thread, the statement is good ~

Call it

Java Thread Learning Note (two) thread exception handling

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.