File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions (Khalid & Rolf 's book) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions (Khalid & Rolf Watch "Exceptions (Khalid & Rolf New topic
Author

Exceptions (Khalid & Rolf 's book)

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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?

Thanks
Vanitha.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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

will not compile.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
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
--------------------------------------------------------------

Thanks,
Vanitha.
INDU, BALA
Ranch Hand

Joined: Jun 07, 2001
Posts: 48
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
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.
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
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
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
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.
Richard Agnes
Greenhorn

Joined: Feb 12, 2001
Posts: 22
code:
import java.io.*;

public class jsource
{
public static void main (String argv[])
{
try{
new jsource().aMethod();
}catch(Exception e)
{
//statement
}catch(IOException e)
{
//not reachable
}
}
void aMethod () throws IOException
{
throw new IOException();
}
}

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions (Khalid & Rolf 's book)