• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in overriden method  RSS feed

 
Vinayak Kulkarni
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is known that the overridden method in a subclass can throw only those checked exceptions, that are there in the throws caluse of the original method in the super class or a part of it. Can anyone explain me why this condition is imposed ?

ie
1. Why the overridden method can't throw other checked exceptions which are not the part of the throws clause of the method in super class?

2. Why the subset of the exceptions exceptions are allowed in the overridden method and Why not only the exact checked exceptions ?

Please help me understanding this concept with detailed explanation. Thank you.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the following example.

For unchecked exceptions, this problem does not exist, because the compiler doesn't have to check if you handle them.

For your second question: Exceptions are just classes, which are organized in a class hierarchy. If your method in the subclass throws an exception that's a subclass of the exception of the super method, then there's no problem. For example, this would be legal:

The compiler just has to check for IOException; since a FileNotFoundException is an IOException, it works without problems.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,
I got what you are saying,but what if the overridden method in the subclass somehow throws a checked exception,due to code logic, which is bigger than the one declared in the base class?
Now if we use the base class' reference, what will happen?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:I got what you are saying,but what if the overridden method in the subclass somehow throws a checked exception,due to code logic, which is bigger than the one declared in the base class?
Now if we use the base class' reference, what will happen?

obviously compiler will complaint! the remedy is wrap the checked exception into runtime exception and then throw the runtime exception.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Sudhanshu
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definition of refinement: Whenever the refined substitution is feasible, the refining substitution must be feasible. You are refining the superclass method by using the subclass method. So the overriding method refines the overridden method. By adding a new Exception, you are saying, "Everybody thinks this method is feasible if you pass 999 as an argument, but in this subclass I am going to make it infeasible."

Liskov Substitution Principle (LSP): A subtype is a subtype if all the methods of the supertype are available without "surprises". The user will think, "I can use method foo() of class Baa because Baa extends Baz and I can always use the foo() method of Baz." Then he suffers an Exception

You can slip unchecked Exceptions past the compiler, but that is still bad design. It is simply that the compiler lacks a mechanism for monitoring unchecked Exceptions. If you are "throwing an Exception ... bigger" than the superclass Exception, then you are not refining the superclass method and you are breaching the LSP.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . . the remedy is wrap the checked exception into runtime exception and then throw the runtime exception.


Bad design! The remedy is to work out why you want an additional Exception in the first place, and get rid of it. If you need Exception wrapping, you have mis-designed your overriding method.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote:Definition of refinement: . . .
Liskov Substitution Principle (LSP): . . .
And Ogeh Ikem said something similar about subtypes and substitutes here. Have a quick look at that post; it's good.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell:well I agree with the LSP.however lets pretend you are overriding a super class method in a subclass

so how will you handle this situation?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!