• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

catching exception from parent's constructor  RSS feed

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an interesting design-related problem I've encountered and I'd like to see what people say about it:
I understand that using super() to explicity invoke your parent's constructor must be the 1st line in a constructor. What if your parent throws an exception in the constructor and you want to handle the exception in your constructor? Something like:

Javac complains, stating the call to super must be the 1st statement. Is there a way around this, or can a general design principle be "any exception your parent throws should just be thrown by you"?
- Rolf.
author and iconoclast
Posts: 24203
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no way to do what you're asking for. By definition, if the parent object can't be constructed, then the child object can't meaningfully be constructed. I understand that you can easily come up with scenarios where this isn't necessarily the case; but if you think about it, anything the child class can do to "fix" the problem, the parent class could have done also, if it wanted to, and so either a) the parent class is badly coded and should be fixed, or b) the author of the parent class felt, for one reason or another, that the exception should terminate any attempt to construct an instance of the class. Java treats this sort of thing (rightly so) as a security issue, and won't let you get around it.
Space pants. Tiny ad:
how do I do my own kindle-like thing - without amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!