• 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

Exception handling and inheritance

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:




 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That answers my questions. thank you very much!
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You’re welcome
     
    reply
      Bookmark Topic Watch Topic
    • New Topic