Getting error while using a throws clause with an overriding method in java? [duplicate]

浪子不回头ぞ 提交于 2020-01-21 20:09:14

问题


I am getting a error when i am using a throw clause with the method demo().And i want to know that what are the limitations of using throws in inheritance.

The error is: Exception ClassNotFoundException is not compatible with throws clause in Test.demo().

Class Test
{
    public void demo() throws NumberFormatException, ArrayIndexOutOfBoundsException//error 
    {
        //something here
    }
    public void demo(String s)
    {
        //something here
    }
}//end of Test class

public class MyTest extends Test 
{
    public void demo() throws IndexOutOfBoundsException,ClassNotFoundException
    {
        //something here
    }
    public static void main(String[] args) 
    {

    }
}

回答1:


ClassNotFoundException is a checked exception - and you can't declare that a method override will throw any checked exceptions that the method it's overriding doesn't declare.

From section 8.4.8.3 of the JLS:

A method that overrides or hides another method, including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

Consider, for example:

Test t = new MyTest();
t.demo();

There's nothing in that code to indicate that ClassNotFoundException will be thrown, because Test.demo() doesn't declare that it will be thrown. However, the whole point of checked exceptions is that the caller is forced to consider what to do with them (catch them or declare that they might throw the exception too). The ability to override a method and declare that it throws a new checked exception not covered by the original method declaration would make a nonsense of that.




回答2:


It raises the compiler error because you break one of the core principles of inheritance in Java (and in OOP proper). This principle is the overriding member cannot impose more restrictions to the caller than the overridden member, as declared in the super class, even if we discuss about access type or declared exception types.

In your case, ClassNotFoundException is not declared in the super class, nor any of its ancestors.




回答3:


When you override a method in child class then the throws clause must be compatible with the overridden method. In your case your Test.demo() method throws NumberFormatException, ArrayIndexOutOfBoundsException which are not mandatory for the throws clause because they are RuntimeExceptions

In your MyTest.demo() method you are throwing IndexOutOfBoundsException,ClassNotFoundException out of which IndexOutOfBoundsException is a RuntimeException and that is again not mandatory to throw. ClasssNotFoundException is a checked exception and there is nothing in your parent class demo() method that could match it. hence the error you are getting.

Two options to correct:

  1. add ClassNotFoundException to your Test.demo()
  2. remove it from MyTest.demo()

Hope this helps



来源:https://stackoverflow.com/questions/23780119/getting-error-while-using-a-throws-clause-with-an-overriding-method-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!