当我从 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 ofObject
) or one of its subclasses.
Throwable
and all its subclasses are, collectively, the exception classes.The classes
Exception
andError
are direct subclasses ofThrowable
:
Exception
is the superclass of all the exceptions from which ordinary programs may wish to recover.The class
RuntimeException
is a direct subclass ofException
.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/