• 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

exceptions thrown by overriding members vs exceptions thrown by constructors

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone explain the difference between the two and what are the rules governing these.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exceptions thown by an overriding method can be the same or subclasses of the ones declared in the overriden method. The overriding method could choose also not to throw any of those thrown by the overriden method. These rules ensure that if an object of a subclass is past as an argument to a method waiting to receive an instance of the superclass, the methods invoked on the argument are not to throw any exceptions for which the code (written for the superclass type) is not prepared to cath. The goal of these rules are not break the polymorphism. That is, an instance of the subclass can be used anywhere an instance of the superclass can.
The rules for a constructor in a subclass are quite the opposite to those explained above. A constructor in a subclass can throw any exception not mentioned in the constructor of the superclass. The reason: an instance of the sublcass, being really a different object than an instance of a the superclass could throw any exception not related to the construction of an instance of the superclass. However an instance of the subclass must declare every exception also declared in the constructor of the superclass, or a supertype of those. The reason: constructing a subclass instance involves the construction of the part of the superclass that the subclass has. You are not allowed to catch the exceptions thrown by a call to a superconstructor, because the super(...) must be the first sentence in the constructor, it is not possible to place it within a try clause. If the construction of that "superclass part" fails and the construction were to go on, by cathing an parent constructor exception (and thus allowing not declare it), we would end up with a possible partly constructed object. That is, if the call to the superconstructor throws an exception, the construction process must stop, otherwise a subclass instance partially bad-initialized would be created.
These rules apply only to checked exceptions.
 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic