请解释一下Java中的RuntimeException以及应该在哪里使用它
我正在关注 SO 上的这个精彩讨论,标题为: 针对检查异常的案例 ,但是我无法理解 RuntimeException 到底应该在哪里使用以及它与普通异常及其子类有何不同。谷歌搜索给了我一个复杂的答案,即它应该用于处理编程逻辑错误,并且应该在通常不应该发生异常的情况下抛出,例如在 switch-case 构造的默认块中。
您能在这里更详细地解释 RuntimeException 吗?谢谢。
I am following this great discussion at SO, titled: The case against checked exceptions , but I am unable to follow where exactly RuntimeException should be used and how it is different from normal Exceptions and its subclasses. Googling gave me a complex answer, that is, it should be used to deal with programming logic errors and should be thrown when no Exception should normally occur, such as in the default block of switch-case construct.
Can you please explain RuntimeException in greater detail here. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引自《Effective Java 第二版》,第 58 条:对可恢复条件使用检查异常,对编程错误使用运行时异常
下面是一个示例:
FileNotFoundException
是一个已检查的异常。null
字符串作为文件名,则NullPointerException
(或者可能是IllegalArgumentException
——另一个有争议的争论)应该被抛出。 API 的客户端应该提供有效的字符串值;null
不是。就 API 而言,这是一个程序员错误,很容易预防。这两个异常都是运行时异常。第 59 条:避免不必要地使用已检查异常还提供了额外的指导:
因此,这里是《Effective Java 第二版》的建议的简短摘要:
另请参阅
技术定义
未经检查的异常是定义为
RuntimeException
及其子类,以及Error
及其子类。它们不必在方法的throws
子句中声明。参考文献
相关问题
IllegalArgumentException
或
?null
参数的 NullPointerExceptionQuotes from Effective Java 2nd Edition, Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Here's an example:
FileNotFoundException
is a checked exception.null
string as a filename, thenNullPointerException
(or perhaps anIllegalArgumentException
-- another contentious debate) should be thrown. Client of the API is supposed to provide a valid string value;null
isn't. As far as the API is concerned, this is a programmer error, which was easily preventable. Both of these exceptions are runtime exceptions.Item 59: Avoid unnecessary use of checked exceptions also provides additional guidance:
So here's a short summary of the recommendation from Effective Java 2nd Edition:
See also
Technical definition
An unchecked exception is defined as
RuntimeException
and its subclasses, andError
and its subclasses. They do not have to be declared in a method'sthrows
clause.References
Related questions
IllegalArgumentException
orNullPointerException
for anull
parameter?这可能是因为您正在查看一个争论,即人们在这一点上存在分歧。
非常简单:
Exception
的所有子类(除了RuntimeException
及其子类)都被检查,即编译器将拒绝该代码,除非您捕获或声明它们在方法签名中。但是,RuntimeException
的子类未经检查。这是传统观点,即对于程序可以有效处理的所有内容,您应该使用检查异常,因为这样编译器将强制您处理它们。相反,程序通常不能有效地处理程序员的错误,因此不必检查它们。这就是 Java 标准 API 使用 RuntimeException 的方式。
您链接到的讨论是由一些人(包括我)的观点引发的,他们认为检查的异常会导致错误的代码,因此不应使用。由于您无法在编译器中禁用异常检查,因此唯一的方法是仅使用
RuntimeException
及其子类。IMO 支持这种观点的一个观察是,“仅在程序员错误时使用未检查的异常”的传统智慧实际上主要是向后推理的合理化:没有代码安全原因为什么编译器不应该强迫你与程序员打交道错误。然而,像 NullPointerException 和 ArrayIndexOutOfBoundsException 之类的异常几乎可以在任何地方出现,如果检查了这些异常,就没有人愿意用 Java 进行编程。因此,语言设计者必须为这些例外,并让它们不受检查。为了解释这一点,他们提出了“未经检查的异常是针对程序员错误的”故事。
That's probably because you are looking at an argument, i.e. people are disagreeing about exactly this point.
Very simple: All subclasses of
Exception
(except forRuntimeException
and its subclasses) are checked i.e. the compiler will reject the code unelss you catch or declare them in the method signature. However, subclasses ofRuntimeException
are unchecked.This is the conventional wisdom, which says that for everything that a program can usefully deal with, you should use checked exceptions because then the compiler will force you to deal with them. Conversely, programs can typically not deal usefully with programmer errors, thus they don't have to be checked. This is how the Java Standard API uses
RuntimeException
.The discussion you linked to is sparked by the view of some people (this includes me) who think that checked exceptions lead to bad code and should therefore not be used. Since you can't disable exception checking in the compiler, the only way to do this is to use only
RuntimeException
and its subclasses.One observation that IMO supports this view is that the conventional wisdom of "use unchecked exceptions only for programmer error" is in fact mainly a rationalization of backwards-reasoning: there is no code safety reason why the compiler should not force you to deal with programmer errors. However, something like
NullPointerException
andArrayIndexOutOfBoundsException
can crop up almost anywhere, and if those were checked, nobody would ever want to program in Java. Thus, the language designers had to make a, huh, exception for those, and make them unchecked. To explain this, they came up with the "unchecked exceptions are for programmer errors" story.