java - 如果在调用链上抛出异常,它的所有子类也会抛出异常吗?

标签 java exception

我辅导的一位学生使用的一本 Java 书籍包含以下内容(Blue Pelican Java 版本 3.0.5b Charles Cook):

If you have a method that throws an IOException up to the next level in the calling chain, we should be aware that in addition to IOException being thrown to the next level, all its subclasses are also thrown to the next level in the chain.

这显然是错误的,但它是正确的吗?该陈述的一个版本出现在学生的考试中,并且被认为是真实的。

此外,以下代码似乎是该声明的反例:

public class ExceptionsTester {
    public static void main(String[] args) {
        try {
            throw new IOException();
        } catch (FileNotFoundException e) {
        System.out.println("Subclasses thrown");
        } catch (IOException e) {
        System.out.println("Subclasses not thrown");
        }
    }
}

最佳答案

这本书的意思是:“如果一个方法声明它抛出异常,那么它的所有子类都可以抛出异常”?因为在 Java 中确实如此,但它并没有告诉您任何有关捕获异常及其子类的行为的信息(正如另一个答案所暗示的那样)

关于java - 如果在调用链上抛出异常,它的所有子类也会抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14721017/

相关文章:

python - 错误处理的Python问题(尝试,除外)

c++ - 如何捕获 argv null 异常?

java - 在输入中为字符设置异常以退出仅接受整数且不停止的程序

java - 在数组之前用 2 个对象解析 JSON

java - 我的 JSpinner 不更改也不接受我对其所做的任何更改

java - 在java中初始化自定义类的二维数组

iOS:防止库崩溃整个应用程序

java - iText 是否有任何 Java PDF 创建替代方案?

java - 将 SQLite BLOB 转换为字节数组 (POJO) 的通用方法

c++ - 如何在 C++ 程序中获取错误行号