Exception thrown when Java exception override method

Source: Internet
Author: User
Tags throw exception

Attention:

Subclasses override parent class methods to throw exceptions that are consistent with the parent class, or do not throw exceptions.

Subclasses overriding the parent class method throws an exception that cannot go beyond the scope of the parent class.

  The overriding method cannot throw a new inspected exception or a more general exception than the overridden method, and can throw a non-inspected exception (the parent class throws only the exceptionthat is being inspected). For example: A method of the parent class throws a IOException exception, overriding the method cannot throw exception, can only throw a IOException subclass exception, or throw a non-examined exception. Error and RuntimeException and their subclasses belong to non-inspected exceptions (unchecked), and other exceptions are examined exceptions (checked).

1 classA {2      Public voidFun ()throwsException {3           4     }  5 }  6 classBextendsA {7      Public voidFun ()throwsIOException, RuntimeException {8           9     }  Ten}

The exception thrown by the parent class contains all exceptions, which are spelled correctly.

1 classA {2      Public voidFun ()throwsRuntimeException {3           4     }  5 }  6 classBextendsA {7      Public voidFun ()throwsIOException, RuntimeException {8           9     }  Ten}

Subclass IOException is out of the category of the exception of the parent class, the above syntax is wrong.

1 classA {2      Public voidFun ()throwsIOException {3           4     }  5 }  6 classBextendsA {7      Public voidFun ()throwsIOException, RuntimeException, arithmeticexception {8           9     }  Ten}

RuntimeException is not part of the IO category, beyond the category of the parent category. RuntimeException and ArithmeticException are run-time exceptions, and subclasses override methods can throw any run-time exceptions. So the above wording is correct.

Resources

An issue that throws an exception when overriding a method in inheritance

Exception thrown when Java exception override 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.