I have code:
class Father {
public Father() throws IOException {}
public void foo() throws IOException {}
}
class Child extends Father {
//If use FileNotFoundException here, compile fail
public Child() throws Exception {}
//If use Exception here, compile fail
public void foo() throws FileNotFoundException {}
}
When writing child's constructor, we must throw at least exception of father's constructor or super exception (IOException -> Exception). However, to child's method, it must throw child exception (IOException -> FileNotFoundException). Why it occurs in Java. Please explain it? Thanks
Childcan't havethrows FileNotFoundExceptioninstead ofthrows IOException?super()call therefore it must at least declare the exceptions of the parent constructor. This is not the case when you overwrite a method and do not invoke the super method.