Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java programming
communitycreator

What is Thread getUncaughtExceptionHandler() 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.

The Thread.getUncaughtExceptionHandler() function from the java.lang.Thread class is used to return the handler. It is invoked when the thread is terminated because of an uncaught exception.

It is used for exception handling in threads.

Syntax


Thread.UncaughtExceptionHandler getUncuaghtExceptionHandler()

Parameters

This function does not take any argument values.

Return value

Thread.UncaughtExceptionHandler: This function returns an object of the UncaughtExceptionHandler type.

Example

We can understand the working of Thread.getUncaughtExceptionHandler() through the following code.

// Load library
import java.lang.Thread;
public class Thread_example implements Runnable {
Thread thrd;
public Thread_example() {
thrd = new Thread(this);
// calling run() method
thrd.start();
}
public void run() {
// printing the name of thread
System.out.println("Thread :" + thrd.getName());
/* will return the handler invoked when the thread is stopped because of an uncaught exception. */
Thread.UncaughtExceptionHandler handler =
thrd.getUncaughtExceptionHandler();
System.out.println(handler);
}
public static void main(String[] args) {
new Thread_example();
new Thread_example();
}
}

RELATED TAGS

java programming
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