Java Data Type Tutorial - Java Thread .set Default Uncaught Exception Handler (Thread .Uncaught Exception Handler eh)








Syntax

Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) has the following syntax.

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

Example

In the following code shows how to use Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method.

/*from  w  w w .  j  a va2 s  . c  o m*/
public class Main {

   public static void main(String[] args) {
  
     Thread t = new Thread(new MyThread());
 
     t.setDefaultUncaughtExceptionHandler(new Thread.
     UncaughtExceptionHandler() {
        public void uncaughtException(Thread t, Throwable e) {
           System.out.println(t + " throws exception: " + e);
        }
     });
     t.start();
   }
}

class MyThread implements Runnable {

   public void run() {
      throw new RuntimeException();
   }
}

The code above generates the following result.