SonOfAnException Handling in Try Multi-Catch?

A while back I was doing a presentation when someone in the audience asked me a question about try multi-catch syntax. The initial question was what is the order of processing of exceptions, but the underlying question was what happens if one the exception is a subclass of another exception? Or put another way can the multi-catch handle a Son of an Exception?

I just hate it when someone ask a question I don't have the answer to so it was time to do a little research and see what the answer was. To be clear we are talking about a situation like the following.

try {
  throwAorSonOFA();
} catch(ExceptionA | SonOfExceptionA e) {
  ...
}

Would ExceptionA be thrown or would SonOfExeptionA be caught if SonOfExceptionA was thrown in the try statement?

Well it turn out the answer is neither, because during compilation you would be getting an compile time error.

To find the answer I first started looking in the OpenJDK 7 Features. About a quarter of the way down is the Language section and information JSR 334: Small language enhancements (Project Coin) which was lead by Joe Darcy. Checking through Joe's blog I found an entry on May 24, 2011 on Project Coin: Documentation at Rampdown that had a pointer to the semantics of the Project Coin features as implemented in JDK 7 b143.

In that document you'll find a lot of the reasoning behind the implementation. Specifically you'll find in the Multi-catch and more precise rethrow syntax section the following quote

A try statement may have catch clauses (also called exception handlers).
A catch clause must have exactly one parameter (which is called an exception
parameter); the declared type of the exception parameter can be a single 
class type or can be a union type (denoted with '|') between two or more 
class types (or alternatives). A catch clause with a union type parameter is called
a multi-catch clause. For the purposes of type-checking, a union type D
with alternatives D1, D2... Dn has type lub(D1, D2... Dn) (§15.12.2.7). 

It is a compile-time error if a union type contains two alternatives Di, Dj where Di is a subtype of Dj.

Ok, but you can't take this document as gospel (even though it probably is) so I wondered over to Chapter 14 section 20 of the Java Language Specification, SE 7 Edition. Think of it as the Java Language Bible. Down a ways in bold it more clearly says

It is a compile-time error if a union of types contains two alternatives Di
and Dj (ij) where Di is a subtype of Dj(§4.10.2).
 
  

Ah this is clear. And just for completeness there was also this line that was related to the original question asked.

If a value is thrown and the try statement has one or more catch clauses 
that can catch it, then control will be transferred to the first such catch
clause.

Translation, the exceptions will be tried in order from left to right.

One small little detail I didn't realize is that mutli-catch exception handling is final by default.  The spec says

An exception parameter of a multi-catch clause is implicitly declared final 
if it is not explicitly declared final.
            

And a little further down there is an example of muti-catch that shows what the equivalent code is with the catches being declared as final.

So there you have it, plan and simple. There is no way you can be a Son of an Exception (well at least as far as multi-catch is concerned).

 
  
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Roger Brinkley

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today