如果catch()中允许多个异常,那么它将减少冗余错误处理代码的数量。 例如,
try{
// some statments
}
catch(Type1Exception t1, Type2Exception t2, Type3Exception t3) { // wish if this could be allowed
/* t1, t2, t3 are children of Exception and needs same error handling then why to have different catch blocks with same piece of code */
}
答案 0 :(得分:18)
是的 - 这就是supported in Java 7的原因。
所以你的例子实际上是:
try {
} catch (Type1Exception | Type2Exception | Type3Exception ex) {
...
}