Unreachable catch block
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Below code does not compile complaining "Unreachable catch block for IOException". Can someone please explain, why so ?
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The error message I get (using a 1.5 version of javac) is a bit more helpful:
Your variable "myref" has a type of TechnoSampleSub, and in that class, the overridden version of test() does not throw anything. So when you call myref.test() in the try block, the compiler knows that an IOException will not be thrown, and so the catch block will never be reached.
If you change the reference type to TechnoSample, then your code will compile -- even if the true object type remains TechnoSampleSub...
TechnoSample myref = new TechnoSampleSub();
...exception java.io.IOException is never thrown in body of corresponding try statement...
Your variable "myref" has a type of TechnoSampleSub, and in that class, the overridden version of test() does not throw anything. So when you call myref.test() in the try block, the compiler knows that an IOException will not be thrown, and so the catch block will never be reached.
If you change the reference type to TechnoSample, then your code will compile -- even if the true object type remains TechnoSampleSub...
TechnoSample myref = new TechnoSampleSub();
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shubha Kirani
Greenhorn
Posts: 12
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Marc,
Thanks for your response.
The only difference in the below code is change in Exception type.
This execute, displaying output: 'In TechnoSampleSub'
How does Exception and IOException differ ?
This is true when I tested. But Overriden methods should always refer object type right. In this case why should we look for reference type ?
Why does the compiler behave different ? Not sure if I'm missing some link here.
Thanks for your response.
The only difference in the below code is change in Exception type.
This execute, displaying output: 'In TechnoSampleSub'
How does Exception and IOException differ ?
If you change the reference type to TechnoSample, then your code will compile -- even if the true object type remains TechnoSampleSub...
This is true when I tested. But Overriden methods should always refer object type right. In this case why should we look for reference type ?
Why does the compiler behave different ? Not sure if I'm missing some link here.
posted 18 years ago
Exception is less specific (further up the hierarchy), and includes unchecked RuntimeExceptions. The compiler knows that your method won't throw a checked IOException (so it knows that code catching this will be unreachable), but it's always possible that your method might throw some unexpected RuntimeException, which would be caught by the "wider net" of catch(Exception e).
Yes, overridden methods are invoked based on the true runtime type of the object, and the compiler can't know what the exact runtime type will be.
But if the reference is a subclass type, then the compiler knows that the true runtime type of the object cannot be the superclass. (This might compile with an explicit cast, but it would still fail at runtime.) Therefore, the compiler does not need to consider exceptions thrown by the superclass version of a method. So if the overridden method in the subclass does not throw any checked exceptions, then the compiler knows these exceptions will not be thrown.
On the other hand, if the reference type is the superclass, then the compiler can't know whether the object will be an instance of the superclass or one of its subclasses. So if the superclass version of the method throws an exception, then the compiler knows this is a possibility.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Shubha Kirani:
...How does Exception and IOException differ? ...
Exception is less specific (further up the hierarchy), and includes unchecked RuntimeExceptions. The compiler knows that your method won't throw a checked IOException (so it knows that code catching this will be unreachable), but it's always possible that your method might throw some unexpected RuntimeException, which would be caught by the "wider net" of catch(Exception e).
Originally posted by Shubha Kirani:
...But Overriden methods should always refer object type right. In this case why should we look for reference type? ...
Yes, overridden methods are invoked based on the true runtime type of the object, and the compiler can't know what the exact runtime type will be.
But if the reference is a subclass type, then the compiler knows that the true runtime type of the object cannot be the superclass. (This might compile with an explicit cast, but it would still fail at runtime.) Therefore, the compiler does not need to consider exceptions thrown by the superclass version of a method. So if the overridden method in the subclass does not throw any checked exceptions, then the compiler knows these exceptions will not be thrown.
On the other hand, if the reference type is the superclass, then the compiler can't know whether the object will be an instance of the superclass or one of its subclasses. So if the superclass version of the method throws an exception, then the compiler knows this is a possibility.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shubha Kirani
Greenhorn
Posts: 12
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Marc,
Thanks for the reply. Your explanation was very good to understand.
Thanks for the reply. Your explanation was very good to understand.
| A magnificient life is loaded with tough challenges. En garde tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








