關於UncaughtException
java
問1:
以下代碼 ide
try { //代碼塊 } catch (Throwable e) { e.printStackTrace(); }
/*** * Dispatch an uncaught exception to the handler. This method is * intended to be called only by the JVM. */ private void dispatchUncaughtException(Throwable e) { getUncaughtExceptionHandler().uncaughtException(this, e); }
public class UncaughtExceptionHandlerImpl implements UncaughtExceptionHandler{ @Override public void uncaughtException(Thread t, Throwable e) { //能夠記錄日誌 //能夠釋放資源 //。。。。。。 System.out.println(e); } } public class TestThread extends Thread{ @Override public void run() { int i = 1/0; System.out.println(i); } public static void main(String[] args) { TestThread t = new TestThread(); t.setUncaughtExceptionHandler(new UncaughtExceptionHandlerImpl()); t.start(); } }
問4:UncaughtExceptionHandler 僅抓exception的異常?
答:不是,這個命名有些迷惑人,實際上你們看源碼就知道uncaughtException(Thread t, Throwable e )
入參是Throwable接口,因此java.lang.Error和java.lang.Exception都會捕獲。
同時由於工程對其餘中心或是項目引用比較多,常常會有java.lang.NoClassDefFoundError: Could not initialize class
等相似問題,因此try catch的時候捕獲Throwable比較好
結束語:鑑於上述知識點,建議將全部重要子線程設置UncaughtExceptionHandler,在主線程獲取子線程日誌,不然有問題子線程內部未捕獲,有問題將會石沉大海,沒法定位。
this