Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in overridding

 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method."

what is the reason behind this restriction.

thanks for help.
 
Stary Kapec
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because when you override a method, you must preserve the contract made by the overridden method. In other words, you should not break promises given by the overridden method.



The Superclass.go() method promises the user of the method that it will NOT throw any checked exceptions except the FNFE. Now suppose you override this method and violate the exception specification :



And then, suppose a user does this :

Now, as far as user is concerned, the go() method of var should not throw anything other than FNFE. (Note that var's static type is Superclass). He will write the code that uses var.go() accordingly. However, this would break the declare-or-handle rule for the checked exceptions : The exception that go() is actually throwing is neither declared nor handled by the user.


Hence, in order to avoid surprises, it makes sense that the overridding method doesnot throw any broader checked exceptions than the ones declared by the overridden method.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jasiek and Neelesh ....I agree with you but tell me that if subclass overridding method throw a wider exception (say Exception) whereas the superclass overridden method throw NullPointerException, then compiler should be glad because anyway we are handling NullPointerException (and ofcourse some more exceptions) through "Exception" in subclass overridding method. It ensures that anyhow we are taking care of NullPointerException.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vaibhav Chauhan:
Thanks Jasiek and Neelesh ....I agree with you but tell me that if subclass overridding method throw a wider exception (say Exception) whereas the superclass overridden method throw NullPointerException, then compiler should be glad because anyway we are handling NullPointerException (and ofcourse some more exceptions) through "Exception" in subclass overridding method. It ensures that anyhow we are taking care of NullPointerException.




We are not handling, we are throwing. Remember that this rule talks about the exceptions that are thrown - and by "thrown", we mean exceptions that escape the method.

The overridden method can handle any checked and unchecked exceptions with a try-catch block. The rule that you quoted does not apply to exceptions that are "handled" using a try-catch block.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i was totally confused. now i am clear.
thanks a lot neelesh ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic