Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who actually handles exceptions thrown in the main method?

Tags:

java

exception

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.

like image 669
user3663882 Avatar asked Feb 22 '15 08:02

user3663882


2 Answers

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:

  1. If the current thread has an uncaught exception handler set, then that handler is executed.

  2. Otherwise, the method uncaughtException is invoked for the ThreadGroup that is the parent of the current thread. If the ThreadGroup and its parent ThreadGroups do not override uncaughtException, then the default handler's uncaughtException 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 of ThreadGroup 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 of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.
like image 113
aioobe Avatar answered Nov 06 '22 07:11

aioobe


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, 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 the ThreadGroup that is the parent of the current thread. If the ThreadGroup and its parent ThreadGroups do not override uncaughtException, then the default handler's uncaughtException method is invoked.

like image 34
RealSkeptic Avatar answered Nov 06 '22 07:11

RealSkeptic