Exceptions (Khalid & Rolf 's book)

Hi, Can anyone explain the following lines with an example? Page 158 ---------------------------------------------------------- The Compiler also complains if a catch block for a superclass exception shadows the catch block for a subclass exception, as the catch block for a subclass exception will never be executed. example code given : //compiler complains catch(Exception e){System.out.print(e);} //superclass catch(ArithmeticException e) {System.out.println(e);}//supclass -------------------------------------------------------------- I wrote this little code. I don't get compiler error, what am I missing?

I do not have this book, but my guess is by "shadowing" the authors mean "catch block for a superclass exception placed before catch block for a subclass exception within the same try-catch construct" try { ... } catch(Exception e){System.out.print(e);} //superclass catch(ArithmeticException e) {System.out.println(e);}//subclass

Thanks for your reply Mapraputa. But still I am not clear what authors are trying to say in those lines. In my code I have overridden the division method, in superclass Exception e is catched, and in subclass ArithmeticException is catched. ---> It works fine. In subclass if I place catch(Exception e) before catch block of Arith then compiler error occurs, I understand that second block can't be reached so we get error. If we do like this in any method, we will get this error.

I have given my code in my previous post. Can anyone explain the following lines with an example? ---------------------------------------------------------- The Compiler also complains if a catch block for a superclass exception shadows the catch block for a subclass exception, as the catch block for a subclass exception will never be executed. example code given : //compiler complains catch(Exception e){System.out.print(e);} //superclass catch(ArithmeticException e) {System.out.println(e);}//supclass --------------------------------------------------------------

Hi vanitha, when you use multiple catch statements, Exception subclasses must come before any of their super classes because the catch statement that uses super class will catch exceptions of that type plus any of its subclasses. So a subclass would never be reached if it came after super class for eg in this code Arithmetic exception is a subclass of EXception . so The first catch statement handle all exceptions including arithmetic exceptions.If you reverse the order it will work fine. catch(Exception e){System.out.print(e);} //superclass catch(ArithmeticException e) {System.out.println(e);}//supclass

As far as the overriding methods are concerned

An overriding method cannot throw more exceptions than were declared for the original method. That is why your code is working perfectly.Hope this is ok. If not please clarify.

thanks

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356

posted Jul 05, 2001 08:43:00

0

Thanks for your reply Indu. catch(Exception e){System.out.print(e);} //superclass catch(ArithmeticException e) {System.out.println(e);}//supclass I tried this in my code, it is not giving any error. In my code I have overridden the method discussion, please take a look at it. Vanitha.

Vanitha, I think there is some ambiguity in the quote you originally cited. The superclass/subclass referenced there is pertaining to the hierarchy of Exceptions as caught within a single try/catch block, not to principles of overridding methods which include try/catch blocks within your own super/sub classes. Your sample code is attempting to override a method with a try/catch in the superclass with a subclass method which catches a different exception. This is not the situation that the quote is trying to illustrate, although it's easy to see where it might be confusing.

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356

posted Jul 05, 2001 09:29:00

0

Thanks for your reply Scott. Can you give me an example code for the quote? I want to write a code and check these lines. Vanitha.

[This message has been edited by Vanitha Sugumaran (edited July 05, 2001).]

INDU, BALA
Ranch Hand

Joined: Jun 07, 2001
Posts: 48

posted Jul 05, 2001 13:28:00

0

Hi Vanitha, Scott has given clear explanation on using super and subclass exceptions in single try catch block. Try to code both exceptions in the same try catch block and run the program. It will clear your doubt.

IOException is a subclass of Exception. The rule is that the exception is caught in the first catch handler, therefore the second catch in this example is unreachable since the exception is caught by the first catch block. The compiler gives this error. Generally, more specific exceptions should be handled first. clear? cheers. Rich.