What is the Thread getDefaultUncaughtExceptionHandler() in Java?

This static function Thread.getDefaultUncaughtExceptionHandler() from the java.lang package helps to return the default handler.

This exception will be raised only when a running thread abruptly dismisses due to an uncaught exception.

  • If the returned value by this function is null, there is no default.

This method in the Thread class is purely used for exception handling purposes.

Syntax


static Thread.UncaughtExceptionHandler
getDefaultUncaughtExceptionHandler()

Parameters

The function doesn’t take any parameters.

Code

The below-mentioned code helps to show the exception, null, which means there is no default uncaught exception.

// Load Thread class
import java.lang.Thread;
// Main class
public class EdPresso implements Runnable {
Thread thr;
public EdPresso() {
thr = new Thread(this);
//calling run() function
thr.start();
}
// run() method
public void run() {
// printing thread name
System.out.println("Thread is:" + thr.getName());
/* returning the default handler that is invoked when a thread stops due to uncaught exception. */
Thread.UncaughtExceptionHandler handler = Thread
.getDefaultUncaughtExceptionHandler();
System.out.println(handler);
}
public static void main(String[] args) {
new EdPresso();
}
}

Free Resources