A rule that throws a declaration exception when overwriting the original method when inheriting classes or implementing Interfaces

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.