3

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

What Exception type one must assume for the exception variable in Java's new multi-catch construct:

try{
   //-- do error prone stuff
}
catch (ExceptionTypeA | ExceptionTypeB e) {
   //-- e.methodA() or e.methodB() ?
}

If ExceptionTypeA and ExceptionTypeB are custom exceptions with custom utility methods, then, what type is e when writing code ?, what methods can one call upon e ?

Community
  • 1
  • 1
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • 1
    at a time u will catch one exception only so e.printStackTrace(); will show u that exception. – Android Killer Dec 15 '12 at 07:55
  • 1
    i'm guessing either the lowest common ancestor or the base class (Exception/throwable). i dont have access to a compiler right now, but should be possible to check. – radai Dec 15 '12 at 08:01
  • It more like hierarchy from lower to higher (if I am not wrong). – Smit Dec 15 '12 at 08:03

1 Answers1

4

The type of e will be the closest parent type of both ExceptionA and ExceptionB. if they are just extending Exception, then type will be Exception.

If you are calling different methods for different exception cases like methodA if ExceptionA and methodB for ExceptionB you should not be using multi-catch. If both ExceptionA and ExceptionB extends a common supertype and overrides one of it s method, then you could use it.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42