This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Overriding and checked Exceptions - Basic Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding and checked Exceptions - Basic Doubt" Watch "Overriding and checked Exceptions - Basic Doubt" New topic

Overriding and checked Exceptions - Basic Doubt

Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
In answering the previous post on overriding, I got into this doubt. I am not able to reason it . Cud somebody help?
An Overriding method cannot throw any more Checked Exception than the overriden method.
My Question is WHY
I was trying to reason saying that when the object reference of the parent class finds a child object and if there is a overriden method in it which throws more checked Exceptions ...
... then I am lost!!!
I say.. so what ? It is after all an extended functionality ... I am not cutting off any of the parent's functionality .... !!
Where am I going wrong ?
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
Hi Jennifer,
The reason that an overriding method is not allowed throw any more rather different checked expections than the function it is overriding because in a situation where client code wants to treat a hierarchy of objects polymorphically (access methods in all of them as base class ) the client has no way of determining that there is an overidden function in some class in hierarchy which throws some exception which it does not expect to be thrown.
Consider the following code:
for(int i = 0; i < someArray.length; i ++)
catch(ExceptionThrowInBaseClassMethod e){}
where someArray consists of objects of classes derived from same base class. Had it been allowed to add more exceptions, it would make writing such a code impossible.
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Thanks Bhushan ! that settles it!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Overriding and checked Exceptions - Basic Doubt
It's not a secret anymore!