Example 1: When Class B inherits Class:
Class {
Public void show () throws filenotfoundexception {
Bufferedinputstream in = new bufferedinputstream (New fileinputstream ("ad. dat "));
}
}
Class B extends {
Public void show () throws filenotfoundexception, sqlexception {
Bufferedinputstream in = new bufferedinputstream (New fileinputstream ("ad. dat "));
Connection conn = jdbcutils. getconnection ();
}
}
An error is prompted during compilation. The reason is that sqlexception is not thrown in its parent class A in the exception thrown by the show method in B.
Example 2: When Class D implements Interface C:
Interface C {
Void disply ();
}
Class D implements c {
Public void disply () throws filenotfoundexception {
Bufferedinputstream in = new bufferedinputstream (New fileinputstream ("ad. dat "));
}
}
An error will also be prompted during compilation because the display method in D throws the filenotfoundexception exception, and the display method declared in Interface C of its implementation does not throw any exception.
Summary:
A class implements an interface or inherits a class. When the class overwrites the method, the exception thrown can only be an exception of the original method or a subset of the exception of the original method.