aspose file tools*
The moose likes Beginning Java and the fly likes doubt in Exception handling during inheritence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in Exception handling during inheritence" Watch "doubt in Exception handling during inheritence" New topic
Author

doubt in Exception handling during inheritence

chandra mohan
Greenhorn

Joined: May 05, 2008
Posts: 20
Hi,
I had just tried the below code but it gives compilation error.Could any one of you please explain me why?

I am declaring the exception which is defined in the super class.Why it is giving error in class T3 ? It is also subclass of T1 correct ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
T3.t() overrides T2.t() and T2.t() is not declared to throw any Exception, so T3.t() cannot throw any checked exceptions.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
As Joanne has told you. A method can only declare a (checked) Exception if it is the same Exception its superclass method declares (or a subclass thereof).

You must be able to call the subclass method under the same conditions that you call the superclass method (as Barbara Liskov showed), so adding an Exception will violate those conditions.

T1#t declares two Exceptions, and is overridden by T2#t which tells the compiler it has managed to get rid of the Exceptions.
T3#t tries to override T2#t but tells the compiler it has new Exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in Exception handling during inheritence