• 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

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello



OVERRIDING EXCEPTION METHODS
When you are deriving classes an overridden method cannot throw a exception greater then that
declared for the original method. A greater exception is the super exception class of a sub exception
class. Notice we have two hierarchies. The classes that you are defining and the exception classes
they are using. This means if a super class method throws a super class exception (SE) then a sub
class method can only through a super class exception (SE) or a sub class exception (SubE)



May you explain about override exception and INHERITING EXCEPTIONS?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this:



That is what that quote says you can't do.
The overriding method must declare to throw the original exception (as declared in the overriden method: SubException in the example), an exception that is derived from it, or no exception at all. My example is wrong because it declares to throw Exception, which is a base class to SubException.
This rule is necessary because of polymorphism. Somebody might be calling the method in the subclass (MyDerived) through a base class reference (MyBase). They will only be aware of whatever is declared to be thrown in the base version of the method. So code like this:



would be unaware of any thrown Exception that is not SubException or derived!
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A general rule in inheritance: you should be able to use the subclass exactly as you would use the superclass without any surprises. This is attributed to Barbara Liskov.
If you declare an exception in a subclass method which isn't thrown in the superclass, then you have to use the subclass differently from the superclass. So you are not inheriting correctly. So you can only throw an exception in the subclass which is already declared in the superclass. If you have a superclass method which might throw XYZException in a subclass, you have to write "throws XYZException" in the superclass too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic