posted 16 years ago
Take a look at the following three cases.
First, the case where the overriding method throws same exceptions as the original one.
It compiles well.
1.
Second, the case where the overriding method throws more exceptions than the original method. This doesnt compile. Check the message which compiler gives.
It says "fun() in B cannot override fun() in A; overridden method does not throw java.lang.Exception
public void fun() throws ArrayIndexOutOfBoundsException,Exception{"
2.
Lastly, the overriding method throws fewer exceptions than the original.
This also compiles fine.
3.
Now do you have any other probable cases.
Well, thats what we say as that the overriding method can throw lesser or fewer exceptions but can never throw more.
The superclass has no knowledge of the newer exceptions which the sub class is going to throw. And the compiler complains.
Hope it helped.
Thanks,
Sandip