当我们通过扩展 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/