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();
  }
}
Home 
  Java Book 
    Thread Conncurrent  

Thread:
  1. Multithreaded Programming
  2. The Main Thread
  3. Thread Name
  4. Thread sleep
  5. Thread Creation
  6. isAlive( ) and join( )
  7. Thread Priorities
  8. Thread Synchronization
  9. Interthread Communication
  10. Suspending, Resuming, and Stopping Threads
  11. Handle Uncaught Exception
  12. ThreadLocal variables