• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Catching Exceptions that are not thrown - Redux

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to summarize some unusual cases.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic