Who actually handles the exceptions thrown in the main method?

If we throw an exception in the main method and don’t handle it, it will work fine. In fact

 public static void main(String[] args) throws IOException { throw new IOException(); //OK } 

But Java requires that all checked exceptions be handled in the program, so IOException needs to be handled. Who actually handles the IOException in this case?

Note that the Java language specification specifies that an Exception is thrown if it is enclosed with a try block containing a catch clause, the type is a supertype of exception.

+5
source share
3 answers

If you have not taken any special action to catch the exception yourself, ThreadGroup executes ThreadGroup by default.

This is indicated in JLS Chapter 11.3 .

If no catch clause is found that can handle the exception, then the current thread (the thread that encountered the exception) is terminated. Before finalization, all final offers are executed, and the uncaught exception is processed in accordance with the following rules:

  • If an uncaught exception handler is installed in the current thread, this handler is executed.

  • Otherwise, the uncaughtException method uncaughtException called for ThreadGroup , which is the parent of the current thread. If ThreadGroup and its parent ThreadGroups do not override uncaughtException , then the default handler method is called uncaughtException .

In addition, javadoc ThreadGroup.uncaughtException is read as follows:

Called by the Java virtual machine when a thread in this thread group is stopped due to an uncaught exception and the thread does not have a specific Thread.UncaughtExceptionHandler .

The uncaughtException ThreadGroup method performs the following actions:

  • If this thread group has a parent thread group, the uncaughtException method of this parent is called with the same two arguments.
  • Otherwise, this method checks whether the installed exception uncaughtException is installed by default, and if so, its uncaughtException method uncaughtException called with the same two arguments.
  • Otherwise, this method determines whether the Throwable argument is Throwable instance of ThreadDeath . If so, nothing special is being done. Otherwise, a message containing the stream name returned from the getName stream method and the stack backtrack using the Throwable printStackTrace method is printed to the standard error stream.
+9
source

If the exception is not caught, the thread exception handler or thread group is called, and then the thread terminates.

JLS chapter chapter 11 states that:

If no catch clause is found that can handle the exception, then the current thread (the thread that encountered the exception) is terminated. Before completion, all finally conditions are satisfied and the uncaught exception is processed in accordance with the following rules:

If an uncaught exception handler is installed in the current thread, this handler is executed.

Otherwise, the uncaughtException method uncaughtException called for ThreadGroup , which is the parent of the current thread. If ThreadGroup and its parent ThreadGroup do not override uncaughtException , then the default uncaughtException handler method is called.

+2
source

The JVM itself handles the exceptions thrown from main() .

+1
source

Source: https://habr.com/ru/post/1213896/


All Articles