• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling & Overriding Methods

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Khalid's book p.168 question 5.23 discusses overriding a method that throw different exceptions. Method f() in class A throws an ArithmeticException and the same method is overriden to throw an InterruptedException. The answer is (c) the overriding f() method in MyClass is not allowed to throw InterruptedException, since the f() method in class A is not allow to throw this exception. Can someone explain why this is the case? I'm not completely clear on this.
Thanks,
Donald
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overriding method can only throw exceptions that are subclasses of those declared in the method being overridden ie., in the superclass method. Ofcourse they can also throw the same exceptions declared by the superclass method.
Hope that helps!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has a lot to do with the OO principle of substitutability, more formally know as the Liskov Substitution Principle (LSP) which states that Methods that use references to base classes must be able to use objects of derived classes without knowing it.. That is, if B is a subclass of A, then you should be able to use a B in any method that uses an A without having to change your code. This way, polymorphism can be used effectively and correctly. If the subclass B could throw an exception that the superclass A didn't, then any code that substituted an instance of B where an A is used would have to be modified to take care of the "extra baggage" that B brings to the table. This goes against the LSP.
 
Donald Nunn
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your explanations.
Donald
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this rule also apply to Runtime Exceptions?
I mean can a subclass throw a Runtime Exception which is not thrown by the superclass ?
Cheers,
Gokhan
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gokhan,
Runtime exceptions can be thrown at any time, from anywhere. They are not 'checked' by the compiler and are totally ignored for the purpose of overriding.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic