• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Unreachable catch block

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below code does not compile complaining "Unreachable catch block for IOException". Can someone please explain, why so ?

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message I get (using a 1.5 version of javac) is a bit more helpful:

...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();
 
Shubha Kirani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Shubha Kirani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc,
Thanks for the reply. Your explanation was very good to understand.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic