Java Reference
In-Depth Information
Thread
object of the thread that threw the exception, and
e
identifies the
thrown exception or error—perhaps an
OutOfMemoryError
instance was
thrown.Ifthismethodthrowsanexception/error,theexception/errorisignored
by the JVM.
• Assumingthat
setUncaughtExceptionHandler()
wasnotcalledtoin-
stall a handler, the JVM passes control to the associated
ThreadGroup
ob-
ject's
uncaughtException(Thread t, Throwable e)
method.As-
suming that
ThreadGroup
was not extended, and that its
uncaughtEx-
ception()
method was not overridden to handle the exception,
un-
caughtException()
passescontroltotheparent
ThreadGroup
object's
uncaughtException()
methodwhenaparent
ThreadGroup
ispresent.
Otherwise, it checks to see if a default uncaught exception handler has been
installed(via
Thread
's
static void setDefaultUncaughtExcep-
tionHandler(Thread.UncaughtExceptionHandler handler)
method.) If a default uncaught exception handler has been installed, its
un-
caughtException()
methodiscalledwiththesametwoarguments.Other-
wise,
uncaughtException()
checksits
Throwable
argumenttodeterm-
ine if it is an instance of
java.lang.ThreadDeath
. If so, nothing spe-
cialisdone.Otherwise,as
Listing4-20
'sexceptionmessageshows,amessage
containingthethread'sname,asreturnedfromthethread's
getName()
meth-
od, and a stack backtrace, using the
Throwable
argument's
printStack-
Trace()
method, is printed to the standard error stream.
setDefaultUncaughtExceptionHandler()
methods.
Listing 4-21.
Demonstrating uncaught exception handlers
class ExceptionThread
{
public static void main(String[] args)
{
Runnable r = new Runnable()
{
@Override
public void run()
{
int x = 1/0;