您将如何处理重写方法的异常

class Parent{

public void m1() Throws Exception{

         //m1
      }
}

class Child extends Parent{
    public void m1() {
   }
}

以上程序成功编译。但是,我们将如何处理被重写方法引发的异常,因为被重写方法不会引发任何异常。

kingkong1900 回答:您将如何处理重写方法的异常

您必须了解有关checkedunchecked的异常以及覆盖时的异常处理。

基本规则是:

1. If SuperClass does not declare an exception,then the SubClass can only declare unchecked exceptions,but not the checked exceptions.

2. If SuperClass declares an exception,then the SubClass can only declare the child exceptions of the exception declared by the SuperClass,but not any other exception.

3. If SuperClass declares an exception,then the SubClass can declare without exception.

您可以参考https://www.geeksforgeeks.org/exception-handling-with-method-overriding-in-java/以获得更好的理解。

本文链接:https://www.f2er.com/3168740.html

大家都在问