How do I configure a default handler for exceptions that happen in my threads?

John Zukowski

The setUncaughtExceptionHandler() method of Thread, introduced in JDK 5.0, allows you to configure this behavior by letting you attach an implementation of Thread.UncaughtExceptionHandler. The interface offers a single method:

public interface Thread.UncaughtExceptionHandler {
    public void uncaughtException(Thread, Throwable);
}
By attaching a handler to your thread, if an uncaught exception happens (aka runtime exception), the registered handler will respond.
public class MyHandlerTest {
  public static void main(String args[]) throws Exception {
    Thread.UncaughtExceptionHandler handler = new SomeHandler();
    Thread.currentThread().setUncaughtExceptionHandler(handler);
    System.out.println(1 / 0);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact