java - 我知道为什么运行时异常不需要 try - catch 或 throws 但我想知道它是如何设计成不需要它们的?

标签 java exception

当我从 Exception 扩展时,需要 throw 或 catch 。我在 RunTimeException 类的代码中找不到任何内容

最佳答案

这是由 Java Language Specification 规定的在§ 11.2部分:

The unchecked exception classes (§11.1.1) are exempted from compile-time checking.

§11.1.1 指出:

11.1.1. The Kinds of Exceptions

An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.

Throwable and all its subclasses are, collectively, the exception classes.

The classes Exception and Error are direct subclasses of Throwable:

  • Exception is the superclass of all the exceptions from which ordinary programs may wish to recover.

    The class RuntimeException is a direct subclass of Exception. RuntimeException is the superclass of all the exceptions which may be thrown for many reasons during expression evaluation, but from which recovery may still be possible.

    RuntimeException and all its subclasses are, collectively, the run-time exception classes.

  • Error is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover.

    Error and all its subclasses are, collectively, the error classes.

The unchecked exception classes are the run-time exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are Throwable and all its subclasses other than RuntimeException and its subclasses and Error and its subclasses.

关于java - 我知道为什么运行时异常不需要 try - catch 或 throws 但我想知道它是如何设计成不需要它们的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49525709/

相关文章:

java - 多次运行 javafx 应用程序线程

java - 异步 Http 客户端 - 如何使用

java - Java 抛出异常错误

Java - NoClassDefFoundError

java - 链接到 sqlite 数据库的注册按钮

java - Mockito 使用匹配器进行 stub

java - 检查输入以查看 Java 中的输入是否为 double

.net - .net 中的异常处理机制是如何工作的?

Java - 在 contentPane 容器中显示标题

java - 通过 split 返回 java.lang.StringIndexOutOfBoundsException 将前两个单词的第一个字符大写的脚本