Thread setUncaughtExceptionHandler


setuUncaughtExceptionHandlerスレッド実行時異常取得用
スレッドは実行時にchecked異常を投げ出すことはできません.IDEはtry-catchで包むように要求します.したがって、メインスレッドはサブスレッドのスレッド情報を直接取得することができず、各ThreadはsetuUncautExceptionHandlerを介してコールバックインタフェースを登録することができる.
setUncaughtExceptionHandler
public class ThreadUncaughtException {

    public static void main(String[] args) {
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                throw new RuntimeException("wrong");
            }
        };
        t1.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println(t.getName() + "; error=" + e.getMessage());
            }
        });

        t1.start();
    }
}

出力結果
t1; error=wrong

Process finished with exit code 0