• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

overriding method throwing exceptions

 
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 49
1
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80945
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
Campbell Ritchie
Marshal
Posts: 80945
522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic