If we throw an exception in the method main
and don't handle it it will work fine. Actually
public static void main(String[] args) throws IOException {
throw new IOException(); //OK
}
But Java requires any checked exception to be handled in the program, therefore the IOException
should be handled. Who actually handles the IOException in that case?
Note, that the Java Language Specification defines the Exception is handled if it enclosed with a try block containing a catch
clause the type is a supertype of the Exception.
If you haven't taken any special actions to catch the exception yourself, the ThreadGroup
s default uncaughtException
is executed.
This is specified in JLS Chapter 11.3.
If no catch clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated. Before termination, all finally clauses are executed and the uncaught exception is handled according to the following rules:
If the current thread has an uncaught exception handler set, then that handler is executed.
Otherwise, the method
uncaughtException
is invoked for theThreadGroup
that is the parent of the current thread. If theThreadGroup
and its parentThreadGroups
do not overrideuncaughtException
, then the default handler'suncaughtException
method is invoked.
Furthermore the javadoc of ThreadGroup.uncaughtException
reads as follows:
Called by the Java Virtual Machine when a thread in this thread group stops because of an uncaught exception, and the thread does not have a specific
Thread.UncaughtExceptionHandler
installed.The
uncaughtException
method ofThreadGroup
does the following:
- If this thread group has a parent thread group, the
uncaughtException
method of that parent is called with the same two arguments.- Otherwise, this method checks to see if there is a default uncaught exception handler installed, and if so, its
uncaughtException
method is called with the same two arguments.- Otherwise, this method determines if the
Throwable
argument is an instance ofThreadDeath
. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread'sgetName
method, and a stack backtrace, using theThrowable
'sprintStackTrace
method, is printed to the standard error stream.
If the exception is not caught, the thread's or the thread group's uncaught exception handler is invoked, and then the thread terminates.
The JLS chapter 11 states that:
If no
catch
clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated. Before termination, allfinally
clauses are executed and the uncaught exception is handled according to the following rules:If the current thread has an uncaught exception handler set, then that handler is executed.
Otherwise, the method
uncaughtException
is invoked for theThreadGroup
that is the parent of the current thread. If theThreadGroup
and its parentThreadGroup
s do not overrideuncaughtException
, then the default handler'suncaughtException
method is invoked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With