Java:捕获特定异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4566450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:00:07  来源:igfitidea点击:

Java: catching specific Exceptions

java

提问by heysup

say I have the following

说我有以下几点

try{
//something
}catch(Exception generic){
//catch all
}catch(SpecificException se){
//catch specific exception only
}

What would happen when it comes across SpecificException ? Does it catch it as a generic exception first, and then catch the specificexception ?

当遇到 SpecificException 时会发生什么?它是否首先将其作为通用异常捕获,然后再捕获特定异常?

Or does it only catch SpecificException while ignoring generic exceptions.

或者它是否只捕获特定异常而忽略一般异常。

I don't want both generic and specificexception being caught.

我不希望通用和特定异常都被捕获。

采纳答案by Michael Borgwardt

No. All exceptions would be caught by the first block. The second will never be reached (which the compiler recognizes, leading to an error due to unreachable code). If you want to treat SpecificExceptionspecifically, you have to do it the other way round:

不。所有异常都会被第一个块捕获。第二个永远不会到达(编译器识别出,由于无法访问的代码导致错误)。如果你想SpecificException特别对待,你必须反过来做:

}catch(SpecificException se){
//catch specific exception only
}catch(Exception generic){
//catch all
}

Then SpecificExceptionwill be caught by the first block, and all others by the second.

然后SpecificException将被第一个块捕获,所有其他块都被第二个块捕获。

回答by duffymo

This won't compile. You'll be told that the specific exception block isn't reachable.

这不会编译。您会被告知无法访问特定的异常块。

You have to have the more specific exception catch block first, followed by the general one.

您必须首先拥有更具体的异常 catch 块,然后才是通用的。

try
{
    //something
} 
catch(SpecificException se)
{
    //catch specific exception only
}
catch(Exception generic)
{
    //catch all
}

回答by Bozho

This does not compile with eclipse compiler:

这不能用 eclipse 编译器编译:

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

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

So define them the other way. Only the specific one will be caught.

所以用另一种方式定义它们。只有特定的会被捕获。

回答by Neil Bartlett

The catch blocks are tried in order, and the first one that matches the type of the exception is executed. Since Exceptionis the superclass of all exception types, it will always be executed in this instance and the specific cases will never be executed. In fact the compiler is smart enough to notice this and raise a compilation error.

依次尝试 catch 块,并执行第一个匹配异常类型的块。由于Exception是所有异常类型的超类,所以会一直在这个实例中执行,特定情况下永远不会执行。事实上,编译器足够聪明,可以注意到这一点并引发编译错误。

Just reorder the catch clauses.

只需重新排序 catch 子句。

回答by Peter Lawrey

As a side note, the only way to have both catch blocks called is to use nested exceptions.

作为旁注,调用两个 catch 块的唯一方法是使用嵌套异常。

try {
  try{
  //something
  }catch(SpecificException se){
  //catch specific exception only
  throw se;
  }
}catch(Exception generic){
//catch all
}

回答by Bartosz Gurgul

My proposition - catch SQLException and check code.

我的提议 - 捕获 SQLException 并检查代码。

try {
    getConnectionSYS(dbConfigFile, properties);
} catch (SQLException e){
    if (e.getErrorCode()==1017){
        getConnectionUSER(dbConfigFile, properties);
    } else {
        throw e;
    }
}