• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding method throwing exceptions  RSS feed

 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There are things that I really don't understand about the overriding method throwing exceptions. The overriding method can throw exceptions that are subclass of exception thrown by the overridden method. Also Overriding method can throw any runtime exception or error. But I have read also that Overriding method can throw ANY exception. What does that mean? Can it throw a new exception??
 
Krishna Kanth
Ranch Hand
Posts: 49
1
Chrome Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding methods need honour the contract defined its parents. In other words overriding method can throw all exceptions that fit into what ever exceptions parent method declares in its signature.


Apart from these there would be some unforeseen exceptions (RuntimeException) which are not in control of overriding method which could possibly be thrown from child method, but can not be declared.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a good design practice, you should avoid throwing any unchecked exceptions which are not thrown by the superclass' method.

Remember: checked methods: declared with throws keyword, and described in documentation comments.
Unchecked exceptions: described in documentation comments with @throws (or @exception) tag.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!