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"? Thanks. - Rolf.
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.