Skip to content

Instantly share code, notes, and snippets.

@vedraiyani
Last active June 23, 2021 11:41
Show Gist options
  • Save vedraiyani/8216b88a092436e581bf8d831993e885 to your computer and use it in GitHub Desktop.
Save vedraiyani/8216b88a092436e581bf8d831993e885 to your computer and use it in GitHub Desktop.
Demo for default/global exception handler
public class DefaultExceptionHandler implements Runnable {
public void run() {
//throw new RuntimeException();
new Test().err();
}
public static void main(String[] args) {
Thread thread = new Thread(new DefaultExceptionHandler());
thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
public void uncaughtException(Thread thread, Throwable e) {
e.printStackTrace();
System.out.println("Exception caught: " + e);
}
});
// call run() function
thread.start();
}
public class Test{
public void err(){
throw new RuntimeException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment