为什么没有用于调用 static void main() 的 try catch 块
Why is there no try catch block for calling static void main()
当我浏览 JLS 8 11.2 时,我遇到了以下规则:
The Java programming language requires that a program contains
handlers for checked exceptions which can result from execution of a
method or constructor (§8.4.6, §8.8.5). This compile-time checking for
the presence of exception handlers is designed to reduce the number of
exceptions which are not properly handled. For each checked exception
which is a possible result, the throws clause for the method or
constructor must mention the class of that exception or one of the
superclasses of the class of that exception (§11.2.3).
我不明白这条规则的意义。例如,我知道这两个程序
class Main
{
public static void main (String[] args) throws Exception
{
throw new Exception();
}
}
和
class Main
{
public static void main (String[] args) throws Exception
{
try{
throw new Exception();
} catch (Expection e){ }
}
}
格式正确。
但规则要求程序包含任何已检查异常的处理程序,而第一个异常不是这样。其实看看handler的定义:
Every exception is represented by an instance of the class Throwable
or one of its subclasses (§11.1). Such an object can be used to carry
information from the point at which an exception occurs to the handler
that catches it. Handlers are established by catch clauses of try
statements (§14.20).
强调我的。
因此,在第一个程序中没有任何处理程序,但
The Java programming language requires that a program contains
handlers for checked exceptions
你不能解释一下这条规则吗?
How does the JVM handle an exception thrown by the main method 对此进行了详细介绍。
本质上,这是由 JVM 内部完成的 - 因此不受任何 JLS 规则的约束。如果您愿意,可以覆盖此行为。
public static void main(String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("I caught it !" + e);
}
});
throw new Exception();
}
除了 JLS execution chapter that only covers definition of main(). It is possible it doesn't exist, not all of the world is nicely documented. The c code 因为 java 运行程序使用 JNI,我找不到任何正式规范。这应该让您了解发生了什么以及为什么 JLS 在这里无关紧要。
/* Build argument array */
mainArgs = NewPlatformStringArray(env, argv, argc);
if (mainArgs == NULL) {
ReportExceptionDescription(env);
goto leave;
}
/* Invoke main method. */
(*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
/*
* The launcher's exit code (in the absence of calls to
* System.exit) will be non-zero if main threw an exception.
*/
ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
当我浏览 JLS 8 11.2 时,我遇到了以下规则:
The Java programming language requires that a program contains handlers for checked exceptions which can result from execution of a method or constructor (§8.4.6, §8.8.5). This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception (§11.2.3).
我不明白这条规则的意义。例如,我知道这两个程序
class Main
{
public static void main (String[] args) throws Exception
{
throw new Exception();
}
}
和
class Main
{
public static void main (String[] args) throws Exception
{
try{
throw new Exception();
} catch (Expection e){ }
}
}
格式正确。
但规则要求程序包含任何已检查异常的处理程序,而第一个异常不是这样。其实看看handler的定义:
Every exception is represented by an instance of the class Throwable or one of its subclasses (§11.1). Such an object can be used to carry information from the point at which an exception occurs to the handler that catches it. Handlers are established by catch clauses of try statements (§14.20).
强调我的。
因此,在第一个程序中没有任何处理程序,但
The Java programming language requires that a program contains handlers for checked exceptions
你不能解释一下这条规则吗?
How does the JVM handle an exception thrown by the main method 对此进行了详细介绍。
本质上,这是由 JVM 内部完成的 - 因此不受任何 JLS 规则的约束。如果您愿意,可以覆盖此行为。
public static void main(String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("I caught it !" + e);
}
});
throw new Exception();
}
除了 JLS execution chapter that only covers definition of main(). It is possible it doesn't exist, not all of the world is nicely documented. The c code 因为 java 运行程序使用 JNI,我找不到任何正式规范。这应该让您了解发生了什么以及为什么 JLS 在这里无关紧要。
/* Build argument array */
mainArgs = NewPlatformStringArray(env, argv, argc);
if (mainArgs == NULL) {
ReportExceptionDescription(env);
goto leave;
}
/* Invoke main method. */
(*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
/*
* The launcher's exit code (in the absence of calls to
* System.exit) will be non-zero if main threw an exception.
*/
ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;