Is it possible in Java to catch two exceptions in the same catch block?

I need to catch two exceptions because they require the same handling logic. I would like to do something like:

catch (Exception e, ExtendsRuntimeException re) {
// common logic to handle both exceptions
}

Is it possible to avoid duplicating the handler code in each catch block?

163632 次浏览

Java 7及更高版本

从 Java7开始,支持多个异常捕获

语法是:

try {
// stuff
} catch (Exception1 | Exception2 ex) {
// Handle both exceptions
}

ex的静态类型是所列出的异常中最专门化的常见超类型。有一个很好的特性,如果在 catch 中重新抛出 ex,编译器知道只能抛出一个 listed异常。


Java6及更早版本

Prior to Java 7, there are ways to handle this problem, but they tend to be inelegant, and to have limitations.

1号进场

try {
// stuff
} catch (Exception1 ex) {
handleException(ex);
} catch (Exception2 ex) {
handleException(ex);
}


public void handleException(SuperException ex) {
// handle exception here
}

如果异常处理程序需要访问在 try之前声明的本地变量,那么就会出现混乱。如果处理程序方法需要重新抛出异常(并且它已经被检查) ,那么签名就会遇到严重的问题。具体来说,handleException必须声明为抛出 SuperException... ... 这可能意味着您必须更改封闭方法的签名,以此类推。

2号进场

try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
} else {
throw ex;
}
}

再一次,我们在签名方面有一个潜在的问题。

3号入口

try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
}
}

如果遗漏了 else部分(例如,因为目前没有 SuperException的其他子类型) ,代码就会变得更加脆弱。如果异常层次结构被重新组织,那么没有 else的处理程序可能会无声无息地吃掉异常!

如果你不使用 java7,你可以将异常处理提取到一个方法中——这样你至少可以减少重复

try {
// try something
}
catch(ExtendsRuntimeException e) { handleError(e); }
catch(Exception e) { handleError(e); }

Java < = 6.x 只允许捕获每个 catch 块的一个异常:

try {


} catch (ExceptionType name) {


} catch (ExceptionType name) {


}

文件:

每个 catch 块都是一个异常处理程序,并处理 参数类型 ExceptionType, declares the type of exception that the handler can handle and must be 从 Throwable 类继承的类的名称。

对于 Java7,可以在一个 catch 块上捕获多个 Exception:

catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}

文件:

在 JavaSE7和更高版本中,一个 catch 块可以处理多个 type of exception. This feature can reduce code duplication and lessen 抓住一个过于宽泛的例外的诱惑。

参考文献: http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

For Java < 7 you can use if-else along with Exception:

try {
// common logic to handle both exceptions
} catch (Exception ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {


}
else {
throw ex;
// or if you don't want to have to declare Exception use
// throw new RuntimeException(ex);
}
}

编辑并用 Exception 替换 Throwable。

Http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html 涵盖在同一块中捕获多个异常。

 try {
// your code
} catch (Exception1 | Exception2 ex) {
// Handle 2 exceptions in Java 7
}

我在做学习卡,这个帖子很有帮助,只是想说说我的看法。

在 JavaSE7启动之前,我们习惯于使用与 try 块相关联的多个 catch 语句来编写代码。 一个非常基本的例子:

 try {
// some instructions
} catch(ATypeException e) {
} catch(BTypeException e) {
} catch(CTypeException e) {
}

但是现在有了 Java 上的最新更新,我们不再需要编写多个 catch 语句,而是可以在一个 catch 子句中处理多个异常。下面的示例显示了如何实现此特性。

try {
// some instructions
} catch(ATypeException|BTypeException|CTypeException ex) {
throw e;
}

因此,一个 catch 子句中的多个 Exception 不仅简化了代码,而且减少了代码的冗余。 I found this article which explains this feature very well along with its implementation. 从 Java7改进和更好的异常处理 这个也许对你有帮助。