0

可能重复:
在 Java 7 多捕获块中,捕获的异常的类型是什么?

在 Java SE 7 中,可以捕获多种类型的异常:

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

这种语法还有其他用法吗?

我可以用这种语法创建联合吗,比如

public void main() {
    Integer|Boolean a;
    a=true;
    a=Integer.Zero;
}

或者我可以使用它来匿名派生多个接口,比如

public void main() {

    Object o = new List<Integer>|Comparable<List<Integer>>() {
        // here implementing both interfaces...
    }

}
4

1 回答 1

0

The type is the least upper bound of IOException and SQLException, Exception, but with a special rule if you rethrow the exception making sure that the checked exceptions list is IOException and SQLException, not Exception.

See section 14.20 of the Java Language Specification for more precise details - http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20

There isn't really anything similar elsewhere in Java except in choosing the type of the ternary operator.

于 2012-10-13T01:11:59.313 回答