java - 尽管 RuntimeExceptions 扩展了 Exception 类,但编译器如何不检查它们?

标签 java exception jls

当我们通过扩展 Exception 类来创建 CustomException 时,编译器会检查它,但即使 RuntimeExceptions 扩展了 Exception 类,编译器也不会检查它们

这是如何在编译器中建立的。我在 JLS 中找到了一个部分说明原因。

Why Runtime Exceptions are Not Checked
The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers.

幕后是否发生了某些事情,例如标记界面的情况。

我正在寻找一些来源来解释它是如何完成的以及为什么所有检查的异常都没有父类?

最佳答案

Is there something happens behind the scenes like in the case of marker interfaces.

事实上,RuntimeException 不受 Exception 的所有子类都是受检查异常这一规则的约束,这只是规范的一部分。

如何实现该规则的异常(exception)取决于编译器。

关于java - 尽管 RuntimeExceptions 扩展了 Exception 类,但编译器如何不检查它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524627/

相关文章:

java - 如果没有 Thread.sleep(...) 调用,线程代码将无法正常工作

java - 使用 Java Reflection 来确定要实例化哪个类

objective-c - NSKeyedUnarchiver 无法解码类 NSKnownKeysDictionary1 的对象

java - 程序顺序规则在构造函数中起作用之前是否发生?

泛型类的 Java 8 模糊方法引用

java - 使用 itext 7 添加交互式复选框到 PDF

java - 在java中传递-d选项

php - 这种处理PHP错误的方法是否有任何 "gotchas"?

java - OrientDB验证错误

java - 覆盖和弱化访问修饰符