aspose file tools*
The moose likes Beginning Java and the fly likes Why we are not allowed to declare a broader exception while overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why we are not allowed to declare a broader exception while overriding" Watch "Why we are not allowed to declare a broader exception while overriding" New topic
Author

Why we are not allowed to declare a broader exception while overriding

rein dheer
Greenhorn

Joined: Jan 27, 2009
Posts: 1
Hi,

I am learning java and OOPS. Could someone please explain why we are not allowed to declare broader exceptions, decrease the access level for method overridden in the subclass?

Thanks & Regards,
rein
Senthil Kumar
Greenhorn

Joined: Jul 17, 2007
Posts: 21
Hey Rein !

For overriding methods following is a rule :

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. e.g. a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception or any other non-runtime exception unless it’s a subclass of FileNotFoundException.


Please also check this http://www.coderanch.com/t/250880/Programmer-Certification-SCJP/certification/please-confirm-about-overriding-exceptions


Preparing for SCJP 1.6
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
As to why this is the case, an instance of a subclass should be able to be used anywhere an instance of a superclass is expected. (See: Liskov Substitution Principle). Client code might not even know (and should never care) that its dealing with an instance of a particular subclass, so how can it deal with an exception that it doesn't even know could be thrown?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we are not allowed to declare a broader exception while overriding