Java 捕获异常和子类

发布于 2024-11-23 20:03:08 字数 230 浏览 3 评论 0原文

你好,

在 Java 中,如果像 BufferedReader.read() 这样的方法说它可以抛出 IOException 并且我尝试捕获 FileNotFoundException 和 < code>IOException 在两个catch块中,如果文件不存在,会进入什么catch块?

是只输入最具体的还是两者都输入?

Hello,

In Java if a method like BufferedReader.read() says it can throw an IOException and I try to catch a FileNotFoundException and an IOException in two catch blocks, what catch blocks will be entered if the file doesn't exist?

Does it enter only the most specific or both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱给你人给你 2024-11-30 20:03:08

将输入与异常匹配的第一个编码捕获。
编辑以纳入 Azodius 的评论

例如:

try {
   bufferedReader.read();
} catch (FileNotFoundException e) {
   // FileNotFoundException handled here
} catch (IOException e) {
   // Other IOExceptions handled here
}

以下代码无法编译:

try {
   bufferedReader.read();
} catch (IOException e) {
   // All IOExceptions (and of course subclasses of IOException) handled here
} catch (FileNotFoundException e) {
   // Would never enter this block, because FileNotFoundException is a IOException
}

编译器消息显示:

无法访问 FileNotFoundException 的 catch 块。它已经由 IOException 的 catch 块处理

The first coded catch that matches the exception will be entered.
Edited to incorporate comment from Azodius

For example:

try {
   bufferedReader.read();
} catch (FileNotFoundException e) {
   // FileNotFoundException handled here
} catch (IOException e) {
   // Other IOExceptions handled here
}

This following code does not compile:

try {
   bufferedReader.read();
} catch (IOException e) {
   // All IOExceptions (and of course subclasses of IOException) handled here
} catch (FileNotFoundException e) {
   // Would never enter this block, because FileNotFoundException is a IOException
}

Compiler message says:

Unreachable catch block for FileNotFoundException. It is already handled by the catch block for IOException

掌心的温暖 2024-11-30 20:03:08

只有遇到的第一个 catch 块(其中 catch 块的异常类型与抛出的异常类型匹配)才会运行(更具体地说,第一个 catch 块,其中 (e instaceof <异常类型>)==true< /code> 将运行)。其他 catch 块都不会运行。

例如

try{
    BufferedReader.read();
}
catch(FileNotFoundException e){System.out.println("FileNotFoundException");}
catch(IOException e){System.out.println("IOException");}

,如果 BufferedReader.read() 抛出 FileNotFoundException,则会打印 FileNotFoundException

请注意,以下内容实际上并未编译:

try{
    BufferedReader.read();
}
catch(IOException e){System.out.println("IOException");}
catch(FileNotFoundException e){System.out.println("FileNotFoundException");}

因为 Java 意识到不可能捕获 FileNotFoundException,因为所有 FileNotFoundException 也是 IOExceptions.

Only the first catch block encountered where the exception type of the catch block matches the type of the exception being thrown will be run (more specifically, the first catch block where (e instaceof <exception type>)==true will be run). None of the other catch blocks will be run.

For example

try{
    BufferedReader.read();
}
catch(FileNotFoundException e){System.out.println("FileNotFoundException");}
catch(IOException e){System.out.println("IOException");}

Will print FileNotFoundException if BufferedReader.read() throws a FileNotFoundException.

Note that the following doesn't actually compile:

try{
    BufferedReader.read();
}
catch(IOException e){System.out.println("IOException");}
catch(FileNotFoundException e){System.out.println("FileNotFoundException");}

because Java realizes that it is not possible for the FileNotFoundException to be caught because all FileNotFoundExceptions are also IOExceptions.

云柯 2024-11-30 20:03:08

第一个适用于该类型的异常(并且仅适用于该异常)。因此,如果您按照列出的顺序捕获上面的两种异常类型,则会捕获FileNotFoundException

The first one which is suitable for that type of exception (and only that). So if you catch the two exception types above in the order you list them, a FileNotFoundException will be caught.

日暮斜阳 2024-11-30 20:03:08

首先捕获特定的异常。如果在特定异常之前捕获通用异常,则这是一个编译时错误。

Specific exception is caught first. and it's a compile time error if generic exception is caught befor specific one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文