gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:34:00 25 4
gpt4 key购买 nike

我辅导的一位学生使用的一本 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com