Mega Code Archive

 
Categories / Java Book / 007 Thread Conncurrent
 

0389 Handle Uncaught Exception

You can register exception handler by using Thread.UncaughtExceptionHandler and Thread's void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method. public class Main { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { int x = 1 / 0; } }; Thread thd = new Thread(r); Thread.UncaughtExceptionHandler uceh = new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.println("Caught throwable " + e + " for thread " + t); } }; thd.setUncaughtExceptionHandler(uceh); thd.start(); } }