1

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) 
    {

    }
}
gotomanners
  • 7,808
  • 1
  • 24
  • 39
varun
  • 472
  • 4
  • 8
  • 24

3 Answers3

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
0

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

Sanjeev
  • 9,876
  • 2
  • 22
  • 33