Last active
June 23, 2021 11:41
-
-
Save vedraiyani/8216b88a092436e581bf8d831993e885 to your computer and use it in GitHub Desktop.
Demo for default/global exception handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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