Anand's programming Blog

Thoughts and comments on Java software development.

Monday, January 03, 2005

UncaughtExceptionHandler in Java5

Java5 provides a better way to handle the uncaught exception thrown by abnormal thread termination. Now the application can define its own handler which will be called by JVM if the thread is terminated and exception is not caught. The order of sequence is as follows

1) JVM checks if the Thread is handling the exception if not
2) It delegates the responsibility to handle the exception to the ThreadGroup object,if ThreadGroup does not handle the exception, then
3) JVM checks to see if there is Handler( object implementing Thread.UncaughtExceptionHandler) set for the Thread, if so ,JVM delegates the exception to the handler to handle. If no handler is defined for the Thread, then
4) JVM asks the ThreadGroup to use the default handler , if there isn't one, then JVM uses the pre 1.5 way of using the ThreadGroup to deal with handling the exception.

Advantages of defining a default handler:

1) Provides one layer of granularity of controlling the exception
2) Each Thread can have its own default exception handler.

0 Comments:

Post a Comment

<< Home