Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Thread getDefaultUncaughtExceptionHandler() in Java?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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();
}
}

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring