Suppose you can add new excluded exceptions to overridden methods.
class AA{ void method() throws FileNotFoundException{} } class BB extends AA{ @Override void method() throws FileNotFoundException, UnsupportedEncodingException {} }
You are now creating an AA link for the BB object and call method
AA a=new BB(); try { a.method(); } catch (FileNotFoundException e) { e.printStackTrace(); }
The compiler will only allow you to catch a FileNotFoundException
and will not catch a UnsupportedEncodingException
because it is called from an AA reference.
But you can add several types of exceptions from an overridden method
- if they are a subtype of an already generated exception (
IOException
IOException, FileNotFoundException
) because they will be checked, - If a new exception does not need to be checked -> all
RuntimeException
s
source share