0

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

In Java SE 7 it is possible to catch multiple types of exception:

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

Is there any other usage of such syntax?

Can I create unions with this syntax, like

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

or may be I can use this to derive multiple interfaces anonymously, like

public void main() {

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

}
Community
  • 1
  • 1
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 2
    And no, you can't do anything other than catching multiple exceptions at once. – JB Nizet Oct 12 '12 at 20:29
  • @JB why? how long will they coming to it? :) – Suzan Cioc Oct 12 '12 at 20:31
  • 1
    Because the first one would transform Java into a loosely typed language (and that will never happen), and the second one has not been considered, or even submitted, to the project coin. Submit an RFE if you want such a thing to happen, but IMHO, it would make the language more bloated without any significant advantage. – JB Nizet Oct 12 '12 at 20:37
  • No loosely typed. Most specific common ancestor, just like with exceptions. – Suzan Cioc Oct 12 '12 at 20:40
  • Then declare the variable as `Object` directly. – JB Nizet Oct 12 '12 at 21:24
  • What about anonymous inner classes? – Suzan Cioc Oct 12 '12 at 21:26
  • *the second one has not been considered, or even submitted, to the project coin. Submit an RFE if you want such a thing to happen, but IMHO, it would make the language more bloated without any significant advantage.* – JB Nizet Oct 12 '12 at 21:28

1 Answers1

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.

Ricky Clarkson
  • 2,909
  • 1
  • 19
  • 21