Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling and inheritance

 
E. Perry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All!

"In every subclass constructor, the first line of code must call for the superclass constructor"
suppose i want to evaluate if the input i received from the user for the subclass constructor is within my parameters..
the superclass constructor catches a possible exception, but how do i pass it on back to the subclass constructor?
the only option I see, Greenhorn that I am, is to rewrite the exception handling mechanism in every subclass constructor..which I'm sure is not the best way of dealing with this issue
anyway to avoid that? ..Consider the following code:




 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I think you have misunderstood unchecked Exceptions, of which IllegalArgumentException is one. Let’s have a look at a concrete example:-Now, you do not want to create a Person object with a negative age. We all know that is impossible. So what are you going to do? Throw an IllegalArgumentException?
If you catch the Exception, your constructor will complete, and you will have a Person object with 0 as age (and maybe even null for name). That is not right either. So shall we try something else?Now what happens for -999 is that the constructor cannot complete normally, and the object is never created. Because it is an unchecked Exception, it might or might not be caught. But your class invariant is preserved. and that is what you really want.
So,
  • Don’t use a throws declaration for unchecked Exceptions. Only use that for checked Exceptions.
  • Don’t catch an Exception in the same method it is thrown in.
  • Always list all expected Exceptions, checked or unchecked, with the@throws tag in the documentation comments.
  • I hope that helps.
     
    E. Perry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the nice welcome!
    It was very helpful in general, as I am just learning to work with the exception handling mechanism
    although one question remains unanswered:

    what happens to an exception thrown in the superclas constructor?
    does it return to the caller (subclass constructor)
    and if so, how do I catch it, heeding the fact that I can not wrap it in a try/ catch block

    thank you
    Enag
     
    Campbell Ritchie
    Sheriff
    Posts: 48917
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not quite sure, but …
    Exceptions propagate down the call stack, so if the superclass constructor was called from the subclass constructor, you would expect the Exception to pass to the subclass constructor. You can check that by printing the Exception’s stack trace.
    If something has gone wrong in the super class constructor, the subclass object will not be in a consistent state, so you ought not to permit its creation. Let all the constructors pass the Exception on. If it is an unchecked Exception, the compiler will allow you to permit it to propagate to the main method and beyond. But wrap the constructor invocation in a try.It should make no difference whether Person uses super(...); explicitly or not.
     
    E. Perry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That answers my questions. thank you very much!
     
    Campbell Ritchie
    Sheriff
    Posts: 48917
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic