Summary of restrictions on exceptions in java-10.7 from the beginning

Source: Internet
Author: User

Summary of restrictions on exceptions in java-10.7 from the beginning

This section describes the limitations of exceptions.

1. When the parent class has the same method as the interface and the method throws different exceptions at the same time, this is not allowed.

The following is the error code:

package com.ray.ch10;public class Test extends Father implements MyInterface {//Exception AException in throws clause of Father.methodA() is not compatible with MyInterface.methodA()}class AException extends Exception {}class BException extends Exception {}abstract class Father {public void methodA() throws AException {}public void methodB() {}}interface MyInterface {public void methodA() throws BException;public void methodB();}


In the above Code, the methodA method cannot be overwritten in the Test class, because the compiler does not know the exception to be pointed.

 

Correct code: when a parent class or a method of the interface is commented, it can be implemented.

 

2. When the constructor of the parent class throws an exception, the base class must have a constructor that throws the same exception or the parent class of this exception.

package com.ray.ch10;public class Test extends Father {public Test() throws AException {super();// TODO Auto-generated constructor stub}}abstract class Father {public Father() throws AException {}}class AException extends Exception {}


Or

Package com. ray. ch10; public class Test extends Father {public Test () throws Exception {// different places super (); // TODO Auto-generated constructor stub} abstract class Father {public Father () throws AException {}} class AException extends Exception {}


3. When the parent class method does not throw an exception, the method overwritten by the subclass cannot throw an exception.

The following is the error code:

package com.ray.ch10;public class Test extends Father {@Overridepublic void test() throws Exception {// TODO Auto-generated method stub}}abstract class Father {public abstract void test() ;}

 

4. When the parent class method throws an exception, the method overwritten by the subclass does not throw an exception.

package com.ray.ch10;public class Test extends Father {@Overridepublic void test() {// TODO Auto-generated method stub}}abstract class Father {public abstract void test() throws Exception;}


Summary: This chapter briefly summarizes the limitations on exceptions. This is usually prompted by the compiler, so it is not difficult to solve this problem.

 

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.