aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Catching Exceptions that are not thrown - Redux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Catching Exceptions that are not thrown - Redux" Watch "Catching Exceptions that are not thrown - Redux" New topic
Author

Catching Exceptions that are not thrown - Redux

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
I think most of you who tried Dan's mock exam is familiar with this piece of code:


This program will fail because the "second catch clause attempts to catch an exception that is never thrown in the try block.".
Now consider the following code:

Notice that the BException included in the catch phrase is not being thrown, and yet this code will compile properly.

Now correct me if I am wrong here:
I assume that this is allowed because of polymorphism. aMethod() could be overridden and the new method could throw a subclass of the AException (which is allowed). In this case, the the "try" clause in the superclass could still catch that exception.
Which is something like this:

[ June 19, 2003: Message edited by: Alton Hernandez ]
[ June 19, 2003: Message edited by: Alton Hernandez ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Mm, I haven't realy thought about WHY you can catch a thrown exception with a superclass of the exception.
I just assumed that it is related to polymorphism in this sense: You can do this;
SuperClass obj = new SubClass(); // No cast required
So naturally you can throw SubClassException and catch it with reference to SuperClassException. Because SubClassException is-a SuperClassException.
Also, just FYI, though this probably is to esoteric to be on the test, but you can catch Exception even though nothing in the try block throws an exception. That is the one "exception" to the rule, and is why I originally got that question wrong.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Alton, are you familiar with the Java Spec Report?
http://www.ergnosis.com/java-spec-report/java-language/jls-14.20-b.html
They are referring to this part of JLS 14.20:
A catch block C is reachable iff both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

[ June 19, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Marlene Miller:
Alton, are you familiar with the Java Spec Report?
http://www.ergnosis.com/java-spec-report/java-language/jls-14.20-b.html
They are referring to this part of JLS 14.20:

[ June 19, 2003: Message edited by: Marlene Miller ]

Thanks for the link Marlene.
I actually read that section of JLS and had problem understanding the word "assignable". That article help clear things up.
[ June 19, 2003: Message edited by: Alton Hernandez ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Alton for thinking about and sharing your example. It really interested me, because I missed that one on Dan's exam.
[ June 21, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I would like to summarize some unusual cases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Catching Exceptions that are not thrown - Redux