请在Java中解释RuntimeException,并在哪里使用它 [英] Please explain RuntimeException in Java and where it should be used

查看:141
本文介绍了请在Java中解释RuntimeException,并在哪里使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO的这个很棒的讨论中,标题为:针对被检查异常的情况,但是我无法遵循使用RuntimeException的位置,以及它与普通异常及其子类的区别。 Googling给了我一个复杂的答案,也就是说,它应该被用来处理编程逻辑错误,并且应该在没有异常通常发生时抛出,例如在switch-case结构的默认块中。

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.

您可以在这里更详细地说明RuntimeException。谢谢。

Can you please explain RuntimeException in greater detail here. Thanks.

推荐答案


我无法关注哪里应该使用
RuntimeException

I am unable to follow where exactly RuntimeException should be used

这可能是因为你正在看一个参数,即人们对这一点是不同意的。 p>

That's probably because you are looking at an argument, i.e. people are disagreeing about exactly this point.



如何与普通
不同例外及其子类

and how it is different from normal Exceptions and its subclasses.

非常简单:异常的所有子类它的子类)是检查,即编译器会拒绝代码unelss你捕获或在方法签名中声明它们。但是, RuntimeException 的子类未被选中。

Very simple: All subclasses of Exception (except for RuntimeException 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 of RuntimeException are unchecked.


谷歌给了我一个复杂的答案,
,它应该用于处理
与编程逻辑错误,并且
应该抛出,当没有异常
通常会发生,如
默认的开关箱
构造块。

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.

这是传统的智慧,程序可以有效处理的一切,您应该使用检查的异常,因为编译器将强制来处理它们。相反,程序通常不能有效地处理程序员错误,因此它们不必被检查。这是Java标准API如何使用 RuntimeException

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.

您链接的讨论是由一些谁认为检查的例外导致错误的代码,因此不应该被使用的人(包括我)。由于您无法在编译器中禁用异常检查,因此唯一的方法是仅使用 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.

IMO支持这一观点的一个观察是,仅对程序员错误使用未经检查的异常的常规智慧实际上主要是向后推理的合理化:没有代码安全为什么编译器不应该强迫你处理程序员的错误。但是,像 NullPointerException ArrayIndexOutOfBoundsException 之类的东西几乎可以在任何地方出现,如果这些被检查,没有人会想Java程序。因此,语言设计师不得不为这些做出一个例外,并让他们不加以控制。为了解释这一点,他们提出了程序员错误的不加限制的例外的故事。

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 and ArrayIndexOutOfBoundsException 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.

这篇关于请在Java中解释RuntimeException,并在哪里使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆