• 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

Exception in Overriding

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taken from K&B:



Im not very clear why the compiler error is. Also, I feel it contradicts whats said under overriding rules: an overriding method doesn't
have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
Please enlighten me.
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
eat() of Animal throws Exception and eat() of Dog doesn't. So when you call Animal.eat(), you have to handle the exception.
eat() of Dog doesn't have to throw any exception. It cannot throw any exception which is not thrown by eat() of Animal.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler cannot tell at compile time what subtype at runtime might be. So it checks for the maximum possible exceptions.

The subtype her method can throw less if she wants, but the call a.eat() still has to be treated as 'worst possible scenario'. Also here it's pretty obvious that a is actually a dog, but there could be hundreds of line of code between the assigment of a Dog object to a, and the declaration of a as an Animal reference. The compiler just cannot cope with that.
[ August 06, 2007: Message edited by: Marc Wentink ]
 
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 jibs parap:
... Im not very clear why the compiler error is...


In Animal, the eat method declares that it could throw an Exception. So when you call a.eat(), you need to either catch this possible Exception or declare that your main method might throw an Exception.
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you strictly follow the overriding rule, I actually don't need to declare any exception for eat() in the subclass if I don't want to. But in that case, I get an exception. So that means the above overriding rule is insignificant!

Where am I going wrong?
 
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 jibs parap:
If you strictly follow the overriding rule, I actually don't need to declare any exception for eat() in the subclass if I don't want to. But in that case, I get an exception. So that means the above overriding rule is insignificant! ...


The rule means that you do not need to declare an exception in defining the overridding version of eat(). Specifically, you do not need to do this...

[ August 06, 2007: Message edited by: marc weber ]
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is, you are saying you will have to declare the exception when the overriding method is called polymorphically.

If yes, the next puzzle is: compiler doesn't seem to have any knowledge of object its pointing to; so why its worried about the exception declaration of the overriding method.
 
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 jibs parap:
That is, you are saying you will have to declare the exception when the overriding method is called polymorphically...


The compiler doesn't know what the true runtime type of the object will be. But if you call the method using a supertype reference (in this case, Animal), then the compiler knows there is a potential for an exception to be thrown, and this is what you need to take care of.
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc et al; much appreciated.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic