【发布时间】:2021-04-20 09:08:25
【问题描述】:
以下代码在 Java 13 上编译并运行:
public class CheckedExceptionSSCE {
public static void main(String[] args) {
try {
methodNoThrowsDeclaration();
} catch (Exception e) {
// why is this throw allowed?
// no throws in main()
throw e;
}
}
private static void methodNoThrowsDeclaration() {
System.out.println("doesn't throw");
}
}
throw e 怎么会被允许?
它是否在 JLS 中的某处指定?我没找到,可能是我搜索的关键字错误。
编译器是否足够聪明,可以推断出不会抛出真正的检查异常,从而允许代码编译和运行?
【问题讨论】:
-
总会有某种RunTimeException
标签: java exception throw throws