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

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

    }
}
Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow