Use setUncaughtExceptionHandler catch exceptions outside thread

Package com.dwz.concurrency.chapter11;
 / ** 
 * run method of the Thread not throw an exception, only the log console or printed out 
 * / 
public  class the ThreadException {
     Private  Final  static  int A = 10 ;
     Private  Final  static  int B = 0 ; 
    
    public  static  void main (String [] args) { 
        the Thread T = new new the Thread (() -> {
             the try { 
                the Thread.sleep (5_000); 
                int Result = A / B;  
                System.out.println (Result );
            } the catch(InterruptedException E) { 
                e.printStackTrace (); 
            } 
        }); 
        
        // This method can capture exception outside thread 
        t.setUncaughtExceptionHandler ((Thread, E) -> { 
            System.out.println (E); 
            the System.out .println (Thread); 
        }); 
        t.start (); 
    } 
}

After using setUncaughtExceptionHandler exception handling will give setUncaughtExceptionHandler, the thread is no longer inside the abnormal output log information

Guess you like

Origin www.cnblogs.com/zheaven/p/12071458.html