谁实际处理 main 方法中抛出的异常?
Who actually handles exceptions thrown in the main method?
如果我们在方法 main
中抛出异常并且不处理它,它将正常工作。实际上
public static void main(String[] args) throws IOException {
throw new IOException(); //OK
}
但是 Java 要求在程序中处理任何已检查的异常,因此应该处理 IOException
。在这种情况下,谁实际处理 IOException?
请注意,Java 语言规范定义了如果异常包含在包含 catch
子句的 try 块中,则该异常将被处理,该类型是异常的超类型。
JVM 本身处理从 main()
抛出的异常。
如果您自己没有采取任何特殊操作来捕获异常,则执行 ThreadGroup
的默认值 uncaughtException
。
这是在 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 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.
此外 ThreadGroup.uncaughtException
的 javadoc 内容如下:
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.
如果未捕获到异常,则调用线程或线程组的未捕获异常处理程序,然后线程终止。
JLS chapter 11 指出:
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 ThreadGroup
s do not override
uncaughtException
, then the default handler's uncaughtException
method
is invoked.
如果我们在方法 main
中抛出异常并且不处理它,它将正常工作。实际上
public static void main(String[] args) throws IOException {
throw new IOException(); //OK
}
但是 Java 要求在程序中处理任何已检查的异常,因此应该处理 IOException
。在这种情况下,谁实际处理 IOException?
请注意,Java 语言规范定义了如果异常包含在包含 catch
子句的 try 块中,则该异常将被处理,该类型是异常的超类型。
JVM 本身处理从 main()
抛出的异常。
如果您自己没有采取任何特殊操作来捕获异常,则执行 ThreadGroup
的默认值 uncaughtException
。
这是在 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.
此外 ThreadGroup.uncaughtException
的 javadoc 内容如下:
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.
如果未捕获到异常,则调用线程或线程组的未捕获异常处理程序,然后线程终止。
JLS chapter 11 指出:
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.