Register an UncaughtExceptionHandler on a specific thread. - Java Language Basics

Java examples for Language Basics:try catch finally

Description

Register an UncaughtExceptionHandler on a specific thread.

Demo Code

public class Main {
  public static void main(String[] args) {
    Thread.currentThread().setUncaughtExceptionHandler(
        (Thread t, Throwable e) -> {
          System.out.println("In this thread " + t.getName()
              + " an exception was thrown " + e);
        });//w w w.  java2s . c  o  m

    Thread someThread = new Thread(() -> {
      System.out.println(200 / 0);
    });
    someThread.setName("Some Unlucky Thread");
    someThread.start();

    System.out.println("In the main thread " + (200 / 0));
  }
}

Result


Related Tutorials