Exceptions (Khalid & Rolf 's book)
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
Posts: 356
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 356
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
Posts: 356
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).]
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited July 05, 2001).]
INDU, BALA
Ranch Hand
Posts: 48
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| Die Fledermaus does not fear such a tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







