Let’s learn thread getDefaultUncaughtExceptionHandler() method in java.
Thread getDefaultUncaughtExceptionHandler() method in java
getDefaultUncaughtExceptionHandler() method returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. If the returned value is null, there is no default.
Syntax
public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Returns
the default uncaught exception handler for all threads
Example
class ThreadExceptionHandlerDemo implements Runnable { @Override public void run() { System.out.println("Current thread name : " + Thread.currentThread().getName()); } public static void main(String[] args) { ThreadExceptionHandlerDemo teh = new ThreadExceptionHandlerDemo(); Thread t1 = new Thread(teh); Thread t2 = new Thread(teh); t1.start(); t2.start(); Thread.UncaughtExceptionHandler hand = Thread.getDefaultUncaughtExceptionHandler(); System.out.println(hand); } }
Output:
Current thread name : Thread-0
null
Current thread name : Thread-1
Also read – encapsulation in java