• 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 in Inheritance

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,

I am a bit confused.
Can anybody tell me the rules of exception when methods are overridden.

Thanks
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An overriding method can-

Increase the number of unchecked exceptions in the throws clause,

decrease the number of checked exceptions in the throws clause,

shorten an exception type in the throws clause,
for example-
FileNotFoundException is a sub-type of IOException, so if a method throws, IOException, then the overriding method can throw FileNotFoundException or in-fact any sub-type of IOException but not the super-type IOException...
 
Ranch Hand
Posts: 48
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit is correct but still the answer remains incomplete. I will try..

The over ridden method can

- Declare a narrower exception
- Declare the same exception
- Declare no exception at all!
- throw any..just any kind of Run Time Exceptions.

Apart from these 4 points if there is anything else it does, the compilation will fail!

Regards,
Saurabh
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Saurabh Patil:

The over ridden method can

- Declare a narrower exception
- Declare the same exception
- Declare no exception at all!
- throw any..just any kind of Run Time Exceptions.

Apart from these 4 points if there is anything else it does, the compilation will fail!



In this case compilation will succeed although OutOfMemoryError is not a RuntimeException......

It can throw any type of Unchecked Exception
[ September 06, 2008: Message edited by: Ankit Garg ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic