Java Data Type Tutorial - Java Thread .setUncaughtExceptionHandler (Thread .UncaughtExceptionHandler eh)








Syntax

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

public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

Example

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

//from  w ww.  j  av a  2  s .c  o  m
class Main {

  public static void main(String[] args) {

    Thread t = new Thread(new MyThread());
    t.setUncaughtExceptionHandler(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();
  }
}