Lets say it is possible to add new thrown exceptions in overridden methods.
class AA{
void method() throws FileNotFoundException{}
}
class BB extends AA{
@Override
void method() throws FileNotFoundException, UnsupportedEncodingException {}
}
Now you create reference AA to object BB and call method
AA a=new BB();
try {
a.method();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Compilator will let you catch only FileNotFoundException
exception, and wont allow to catch UnsupportedEncodingException
because it is called from reference AA.
But you can add few types of exceptions to overridden method
- if they are subtype of already thrown exception (
IOException
-> IOException, FileNotFoundException
) because they will be checked,
- if new exception doesn't have to be checked -> all
RuntimeException
s