In a Java 7 multicatch block what is the type of the caught exception?

后端 未结 2 824
鱼传尺愫
鱼传尺愫 2020-11-28 14:19

In a Java 7 multicatch block such as the following:

try {
    // code that throws exception
} catch (CharacterCodingException | UnknownServiceException ex) {         


        
2条回答
  •  野性不改
    2020-11-28 14:58

    In JSL 7 http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20-510

    §14.20 > The declared type of an exception parameter that denotes its type as a union with alternatives D1 | D2 | ... | Dn is lub(D1, D2, ..., Dn) (§15.12.2.7).

    The definition of lub() i.e. the least upper bound is quite convoluted. Fortunately types we are talking about here are usually simply non generic subclasses of Throwable, and lub() yields the most specific super class.

    For a more complicated case, consider

    class E1 extends Exception implements G
    class E2 extends Error implements G
    
    lub(E1, E2) = Throwable & G
    

提交回复
热议问题