java - 正确使用 RuntimeException?

标签 java exception runtimeexception

<分区>

Possible Duplicate:
In Java, when should I create a checked exception, and when should it be a runtime exception?

我什么时候应该从 RuntimeException 而不是 Exception 派生异常?

RuntimeException 不必在方法的 throws 子句中声明,这可能,因为它不必特别列出或不好,因为显式声明方法的异常是一种很好的做法。

想法?

最佳答案

来自 Unchecked Exceptions -- The Controversy :

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

请注意,未经检查的异常是从 RuntimeException 派生的异常。并且检查异常是从 Exception 派生的异常.

如果客户端无法从异常中恢复,为什么要抛出 RuntimeException?文章解释:

Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

关于java - 正确使用 RuntimeException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233793/

相关文章:

java - Eclipse JDT 核心 Java 运行时错误 - java.lang.NoSuchMethodError

java - 如何通过不变检查对序列化进行单元测试?

java - Arrays.sort(比较器)- Java 6 与 Java 7

java - 从 JComboBox 的监听器中移除元素

iphone - iOS : won't terminate or handle many exceptions

android - RuntimeException:自定义 TextView 加载字体无法制作原生字体或内存泄漏

excel - java.lang.NoClassDefFoundError : org/apache/poi/ss/usermodel/Workbook

java - 如何在 Android 中使用 Proguard 并保护应用程序的功能完整性?

c# - 我应该抛出 ArgumentException 吗?

c# - 在发生异常时将自定义对象传递给 RunWorkerCompleted 事件