Let’s learn thread setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method in java.
Thread setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method in java
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh) method set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.
Syntax
public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Parameters
ueh the object to use as the default uncaught exception handler. If null then there is no default handler.
Throws
SecurityException – if a security manager is present and it denies RuntimePermission(“setDefaultUncaughtExceptionHandler”)
Example
class SetDefaultUncaughtExceptionHandler { public static void main(String[] args) { Thread t = new Thread(new newThread()); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { System.out.println(t + " exception thrown : " + e); } }); t.start(); } } class newThread implements Runnable { public void run() { throw new RuntimeException(); } }
Output:
Thread[#21,Thread-0,5,main] exception thrown : java.lang.RuntimeException
Also read – arrays in java