Thursday, March 19, 2015

UncaughtExceptionHandler in Java

UncaughtExceptionHandler is interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. Basically, this is a mechanism to catch and treat the unchecked exception thrown in a thread object that what we are going to see in this article.




There are two kinds of exceptions in Java:

  • Checked exceptions: These exceptions must be specified in the throws clause of a method or caught inside them. For example, IOException or classNotFoundException.
  • Unchecked exceptions: These exceptions don't have to be specified or caught. For example, NumberFormatException.

When a checked exception is thrown inside the run() method of a Thread object, we have to catch and treat them, because the run() method doesn't accept a throws clause. When an unchecked exception is thrown inside the run() method of a Thread object, the default behaviour is to write the stack trace in the console and exit the program

Fortunately, Java provides us with a mechanism to catch and treat the unchecked exceptions thrown in a Thread object to avoid the program ending


First of all, we have to implement a class to treat the unchecked exceptions. This class must implement the UncaughtExceptionHandler interface and implement the uncaughtException() method declared in that interface. In our case, call this class ExceptionHandler and make the method to write information about Exception and Thread that threw it. Following is the code:

Now, implement a class that throws an unchecked exception

Now, implement the main class of the example

When an exception is thrown in a thread and is not caught (it has to be an unchecked exception), the JVM checks if the thread has an uncaught exception handler set by the corresponding method. If it has, the JVM invokes this method with the Thread object and Exception as arguments.
If the thread has not got an uncaught exception handler, the JVM prints the stack trace in the console and exits the program.


There is more....
The Thread class has another method related to the process of uncaught exceptions. It's the static method setDefaultUncaughtExceptionHandler() that establishes an exception handler for all the Thread objects in the application. 

When an uncaught exception is thrown in Thread, the JVM looks for three possible handlers for this exception.

First, it looks for the uncaught exception handler of the Thread objects as we learned in this recipe. If this handler doesn't exist, then the JVM looks for the uncaught exception handler for ThreadGroup of the Thread objects as was explained in the Processing uncontrolled exceptions in a group of threads recipe. If this method doesn't exist, the JVM looks for the default uncaught exception handler as we learned in this recipe.

If none of the handlers exits, the JVM writes the stack trace of the exception in the console and exits the program.




If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

1 comment: