Java API 和已检查/未检查的异常混淆

标签 java exception

关于受检异常和未受检异常的问题在这里和其他网站上被提出了数百万次,但我仍然对如此不同的答案感到困惑。

在许多答案和文章中,我们可以读到一般性陈述,例如:

Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time.

和检查异常

represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)

两条引文均来自http://www.javapractices.com/topic/TopicAction.do?Id=129并多次被引用。

我们可以在oracle网站上看到类似的说法:

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 http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

让我们举个简单的例子,用扫描仪读取数字:

Scanner sc = new Scanner(System.in);
int userInput = sc.nextInt();
System.out.println(userInput);

nextInt() 在我键入“asdf”而不是 int 时抛出 InputMismatchException。 InputMismatchException 是 RuntimeException,但根据上面的陈述,这是检查异常“无效用户输入”的第一个示例。

当我查看已检查的异常时,我更加困惑。例如 NPE 与 FileNotFoundException - 两者都可以通过简单的 if-else 来阻止,那么为什么第一个未选中而第二个被选中?

Oracle 网站有底线指南:

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.

所以据此我可以从 FileNotFoundException 或 IOException 中恢复,但我不能从 NPE 或 ArrayIndexOutOfBoundsException 中恢复?这没有意义。

也许有人用更好的例子对此有更好的解释?

最佳答案

非常简单和事实:

  • 未经检查的异常(Runtime 类和子类)表示意外异常。 一般来说,调用者应该不知道如何处理它并且不会在他的任何场景中等待它。

  • checked exception 表示您可能认为是“正常”异常并且调用者知道如何处理它的异常。这对他来说可能不是名义上的情况,但来电者知道它可能会发生并且应该准备好自己处理它。

在你的例子中:

nextInt() throws InputMismatchException when I type i.e. "asdf" instead of int. InputMismatchException is RuntimeException but according to statements above this is the first example of checked exceptions "invalid user input".

当方法调用 nextInt() 时,调用者等待 int 值。这是异常(exception)情况。如果不是这种情况,用户宁愿:next() 检索一个 String 对象,然后尝试将其转换为适合的东西。
当您调用 nextInt() 时,调用者会等待他请求的 int。强制他处理 InputMismatchException 作为已检查的 Exception 是违反直觉的。


So according to that I can recover from FileNotFoundException or IOException but I can't from NPE or ArrayIndexOutOfBoundsException? This makes no sense.

我认为 JDK 异常如 FileNotFoundExceptionIOException 是特例。它们可能被检查不一定是因为这些类的客户端等待文件未找到或流在读取或写入期间生成一些异常,而是因为流可能处于不合适的状态并因此引发许多异常 外部 原因(文件锁定、权限不足、文件移动到另一个地方或无法访问的网络等等...)。 生成异常的文件可能被认为是需要处理的重要文件。 因此,JDK 规范更喜欢调用者显式处理它。

此外,在 try-with-resources 语句到达之前,调用者应明确关闭流以防出现异常。


NPEArrayIndexOutOfBoundsException 一般是编程错误。应用程序无法进行处理以恢复它不知道存在的具体错误。那么为什么要检查它们呢?

关于Java API 和已检查/未检查的异常混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685073/

相关文章:

java - Spring - RestTemplate 抛出 InvalidMediaTypeException

java - 如何使用 IText 在 PDF 页面中间插入内容

java - doInBackground : Will it impact the performance? 内的新线程

java - Java中的局部变量与全局变量同名

c# - Exception.ToString() 的结果在不同的项目中是不同的

java - "Catching exception is not allowed"Checkstyle报告

Java 泛型 : array creation, 类型转换和未经检查的警告

java - 使用单选按钮组时的疑问(JSF 2.0)

iphone - 来自 Core Data : NSInvalidArgumentException, 的隐秘错误原因:referenceData64 仅为抽象类定义

c# - 大多数派生异常是什么?