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 ThreadGroups 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
uncaughtExceptionis invoked for theThreadGroupthat is the parent of the current thread. If theThreadGroupand its parentThreadGroupsdo not overrideuncaughtException, then the default handler'suncaughtExceptionmethod 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.UncaughtExceptionHandlerinstalled.The
uncaughtExceptionmethod ofThreadGroupdoes the following:
- If this thread group has a parent thread group, the
uncaughtExceptionmethod 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
uncaughtExceptionmethod is called with the same two arguments.- Otherwise, this method determines if the
Throwableargument is an instance ofThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread'sgetNamemethod, and a stack backtrace, using theThrowable'sprintStackTracemethod, 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
catchclause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated. Before termination, allfinallyclauses 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
uncaughtExceptionis invoked for theThreadGroupthat is the parent of the current thread. If theThreadGroupand its parentThreadGroups do not overrideuncaughtException, then the default handler'suncaughtExceptionmethod 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