Hello Everyone I am new at this Forum and I wanted to ask a question in this topic-> Exception Handling with Method Overriding in
Java
There is a rule that-> If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.
then why not this code is giving me Compile Time Error->
import java.io.*;
class Parent{
void msg()throws ArrayIndexOutOfBoundsException{System.out.println("parent");}
}
class TestExceptionChild2 extends Parent{
void msg()throws IndexOutOfBoundsException{System.out.println("child");}
public static void main(
String args[]){
Parent p=new TestExceptionChild2();
try{
p.msg();
}catch(Exception e){}
}
}
See I have made Parent Class method msg() to thorw ArrayIndexOutOfBounds Exception which is subclass of IndexOutOfBoundsException and in Derived Class method msg() I have thrown
IndexOutOFBoundsException which is parent of ArrayIndexOutOfBounds . Then why not the code giving me Compile Time Error. Please Help...
Thanks In Advance...