CatchAllThreadExceptionHandler.java Source code

Java tutorial

Introduction

Here is the source code for CatchAllThreadExceptionHandler.java

Source

class CatchAllThreadExceptionHandler implements Thread.UncaughtExceptionHandler {
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("Caught  Exception from  Thread:" + t.getName());
    }
}

public class Main {
    public static void main(String[] args) {
        CatchAllThreadExceptionHandler handler = new CatchAllThreadExceptionHandler();

        // Set an uncaught exception handler for main thread
        Thread.currentThread().setUncaughtExceptionHandler(handler);

        // Throw an exception
        throw new RuntimeException();
    }
}