An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.
In this article, we will understand how to implement Thread.UncaughtExceptionHandler.
Before implementing the handler, let’s understand how exceptions are caused by an example as follows:
The output for the above code is
Exception in thread "main" java.lang.ArithmeticException: / by zero at Demo.main(GFG.java:5)
However, if we wish to override the internal working of JVM such that a custom message is displayed when an exception occurs, we can use Thread.UncaughtExceptionHandler to handle it.
The setDefaultUncaughtExceptionHandler() method of java.lang.Thread class is used to override the way JVM handles uncaught Exceptions.
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
Parameter: This method takes an object of the type UncaughtExceptionHandler as a parameter.
Below are the examples to illustrate the setDefaultUncaughtExceptionHandler() method:
Example 1: Lets try to create a class which implements the interface UncaughtExceptionHandler from the Thread class to handle a division by 0 exception as follows:
Welcome to GeeksforGeeks
Note: The above code doesn’t work on the online IDE because online IDE’s doesn’t give permissions to override the exception handler. Here, the setDefaultUncaughtExceptionHandler() method, changed the field defaultUncaughtExceptionHandler from initial value null to the Random class. The uncaughtException() method of the Random class was invoked, when the uncaught exception occurred.
Example 2: In this example, let’s throw a new exception and understand how the exceptions are handled.
Exception Handled Exception
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Naming a thread and fetching name of current thread in Java
- Difference between Thread.start() and Thread.run() in Java
- How a thread can interrupt an another thread in Java?
- Java.lang.Thread class in Java
- Main thread in Java
- Thread Pools in Java
- Daemon thread in Java
- Inter-thread Communication in Java
- Java Thread Priority in Multithreading
- Thread Safety and how to achieve it in Java
- Lifecycle and States of a Thread in Java
- Lock framework vs Thread synchronization in Java
- Implement Runnable vs Extend Thread in Java
- isAlive() and join() methods of Thread Class in Java
- Thread Interference and Memory Consistency Errors in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.