• 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

Super class constructor throws checked Exception..

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am held up with some doubt, please explain...
Super class constructor is throwing the checked exception, and the sub class constructor is calling the super class constructor. Because super class constructor is throwing the exception, there are two ways to call that constructor from the sub class,
1. sub calss constructor also will throw the exception.
2. use try and catch and call the super class constructor. But it is not working becaus, i can't use the first statement as try in the constructor when i am calling the super constructor in the try block.
Please go throw the below code..

Is there any way for constructor declaration at line number 14.

Any type of thoughts are welcome...

Thanks...
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're right - there is no way to deal with a checked exception thrown in a constructor except to have all constructors that invoke that one (constructors of child classes) throw that exception as well.
However, it would be good to take a step back and think about what is going on here.
If a constructor throws an exception - something went wrong in the creation of that object. As any subclasses of that object are relying upon the parent constructor to initialize inherited members, you can no longer count on your inherited members being initialized properly. Basically, if an exception was thrown in the constructor of a parent class, the class you're currently constructing (the child class) can not be successfully instantiated.
Given that, I think it's only fitting that the only way to deal with an exception thrown in a constructor is to continue to throw that exception all the way to the object that tried to instatiate the class in question.
I hope that helps,
Corey
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey,
I agree with you.
Thanks...
 
reply
    Bookmark Topic Watch Topic
  • New Topic