THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

remain within the thread. If a "parent" thread wants to know why a "child" terminated, the child will have to
store that information somewhere the "parent" can get it. Placing a thread start invocation in a
TRy-catch block does not catch exceptions that may be thrown by the new threadit simply catches any
exception thrown by start.


When an exception is thrown it causes statements to complete abruptly and propagates up the call stack as
each method invocation completes abruptly. If the exception is not caught by the time the run method
completes abruptly then it is, by definition, an uncaught exception. At that point the thread that experienced
the exception has terminated and the exception no longer exists. Because uncaught exceptions are usually a
sign of a serious error, their occurrence needs to be tracked in some way. To enable this, every thread can
have associated with it an instance of UncaughtExceptionHandler. The
UncaughtExceptionHandler interface is a nested interface of class THRead and declares a single
method:


public voiduncaughtException(Thread thr, Throwable exc)

Invoked when thr terminated due to exc being thrown.

A thread's uncaught exception handler can be set, if security permissions allow it, using its
setUncaughtExceptionHandler method.


When a thread is about to terminate due to an uncaught exception, the runtime queries it for its handler by
invoking the getUncaughtExceptionHandler method on it, and invokes the returned handler's
uncaughtException method, passing the thread and the exception as parameters.


The getUncaughtExceptionHandler method will return the handler explicitly set by the thread's
setUncaughtExceptionHandler method, or if no handler has been explicitly set, then the thread's
ThreadGroup object is returned. Once a thread has terminated, and it has no group,
getUncaughtExceptionHandler may return null.


The default implementation of the THReadGroupuncaughtException method invokes
uncaughtException on the group's parent group if there is one. If the thread group has no parent, then
the system is queried for a default uncaught exception handler, using the static Thread class method
getdefaultUncaughtExceptionHandler. If such a default handler exists, its
uncaughtException method is called with the thread and exception. If there is no default handler,
ThreadGroup's uncaughtException method simply invokes the exception's printStackTrace
method to display information about the exception that occurred (unless it is an instance of ThreadDeath in
which case nothing further happens).


An application can control the handling of uncaught exceptions, both for the threads it creates, and threads
created by libraries it uses, at three levels:


At the system level, by setting a default handler using the static Thread class method
setDefaultUncaughtExceptionHandler. This operation is security checked to ensure the
application has permission to control this.


At the group level, by extending ThreadGroup and overriding it's definition of
uncaughtException.



  • At the thread level, by invoking setUncaughtExceptionHandler on that thread.


For example, if you were writing a graphical environment, you might want to display the stack trace in a
window rather than simply print it to System.err, which is where the method printStackTrace puts
its output. You could define your own UncaughtExceptionHandler that implement
uncaughtException to create the window you need and redirect the stack trace into the window.

Free download pdf