Java Reference
In-Depth Information
The class simply prints a message and the thread name stating that an uncaught exception from a thread has
been handled. Typically, you may want to do some cleanup work or log the exception to a file or a database in the
uncaughtException() method of the handler. The thread class contains two methods to set an uncaught exception
handler for a thread: one is a static setDefaultUncaughtExceptionHandler() method and another is a non-static
setUncaughtExceptionHandler() method. Use the static method to set a default handler for all threads in your
application. Use the non-static method to set a handler for a particular thread. When a thread has an uncaught
exception, the following steps are taken:
If the thread sets an uncaught exception handler using the
setUncaughtExceptionHandler()
method, the uncaughtException() method of that handler is invoked.
If a thread does not have an uncaught exception handler set, its thread group's
uncaughtException() method is called. If the thread group has a parent thread group, it
calls the uncaughtException() method of its parent. Otherwise, it checks if there is a default
uncaught exception handler set. If it finds a default uncaught exception handler, it calls the
uncaughtException() method on it. If it does not find a default uncaught exception handler,
a message is printed on the standard error stream. It does not do anything if it does not find a
default uncaught exception handler and a ThreadDeath exception is thrown.
Listing 6-27 demonstrates how to set a handler for uncaught exceptions in a thread. It creates an object of class
CatchAllThreadExceptionHandler and sets it as a handler for the uncaught exceptions for the main thread. The main
thread throws an unchecked exception in its last statement. The output shows that the handler handles the exception
thrown in the main() method.
Listing 6-27. Setting an Uncaught Exception Handler for a Thread
// UncaughtExceptionInThread.java
package com.jdojo.threads;
public class UncaughtExceptionInThread {
public static void main(String[] args) {
CatchAllThreadExceptionHandler handler = new CatchAllThreadExceptionHandler();
// Set an uncaught exception handler for main thread
Thread.currentThread().setUncaughtExceptionHandler(handler);
// Throw an exception
throw new RuntimeException();
}
}
Caught Exception from Thread:main
New Thread Concurrency Packages
Although Java had support for multi-threading built into the language from the very beginning, it was not easy to
develop a multi-threaded Java program that used an advanced level of concurrency constructs. For example, the
synchronized keyword, used to lock an object's monitor, has existed since the beginning. However, a thread that tries to
lock an object's monitor simply blocks if the lock is not available. In this case, a programmer had no choice but to back
out. Wouldn't it be nice to have a construct that is based on a “try and lock” philosophy rather than a “lock or block”
philosophy? In this strategy, if an object's monitor lock is not available, the call to lock the monitor returns immediately.
 
Search WWH ::




Custom Search