Java Thread getDefaultUncaughtExceptionHandler()方法

2019-10-16 22:24:56

Thread類的getDefaultUncaughtExceptionHandler()方法返回當執行緒由於未捕獲的異常而突然終止時呼叫的預設處理程式。 如果返回的值為null,則沒有預設值。

語法

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

返回

  • 此方法返回預設處理程式。

範例

public class JavaetDefaultExceptioneExp implements Runnable   
{  
    public void run()   
    {  
        // prints thread name  
        System.out.println("Currently running thread is: " + Thread.currentThread().getName());  
    }  
    public static void main(String[] args)   
    {  
        JavaetDefaultExceptioneExp g1 = new JavaetDefaultExceptioneExp();  
        Thread t1 = new Thread(g1);  
        Thread t2 = new Thread(g1);  
        // this will call run() function  
        t1.start();  
        t2.start();  

        // returns the default handler  
        Thread.UncaughtExceptionHandler handler = Thread  
         .getDefaultUncaughtExceptionHandler();  
        System.out.println(handler);  
    }  
}

執行上面範例程式碼,得到以下結果:

null
Currently running thread is: Thread-0
Currently running thread is: Thread-1