I am referring to Why can't overriding methods throw exceptions broader than the overriden method?
but How it can be possible in case of UncheckedException
or RunTimeException
to declare a parent exception in child class-
import java.io.*;
class Parent {
void msg() throws NullPointerException {
System.out.println("parent");
}
}
class Child extends Parent {
public static void main(String args[]) {
Parent p = new Child();
try {
p.msg();
} catch (NullPointerException e) {
System.out.println(e);
}
}
void msg() throws RuntimeException {
System.out.println("child");
}
}
Is it since compiler is not responsible to handle with unchecked Exception.
Also one more point here I am handling it with child exception (NullPointerException
) and this is not showing me any compile fail.