posted 18 years ago
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:
try
{
for(int i = 0; i < someArray.length; i ++)
{
someArray[i].someBaseClassOverridenMethod();
}
}
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.
Regards,
Bhushan