• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exception in overriden method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Sudhanshu
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic