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.


//w w w .  j  a v a2  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();
  }
}




















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable