Java Reference
In-Depth Information
Once a thread has terminated, and it has no group,
getUncaughtExcep-
tionHandler
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 meth-
od
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 in-
formation 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 oper-
ation 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 out-
put. You could define your own
UncaughtExceptionHandler
that implement
uncaughtException
to create the window you need and redirect the stack
trace into the window.