我们不能从override方法中抛出已检查的父异常,但是我们可以抛出未经检查的异常,但是从Child2类抛出“Exception”会产生错误(尽管从父类抛出未经检查的异常ArithmeticException)但是我正在尝试抛出RunTimeException然后代码运行正常,更多细节请抛出代码。
class Parent {
void msg() throws ArithmeticException { //ArithmeticException is child of //RuntimeException and both are unchecked exceptions
System.out.println("parent");
}
}
class Child extends Parent {
void msg() throws RuntimeException { //not gives any compile time error
System.out.println("child");
}
}
class Child2 extends Parent {
void msg() throws Exception { //gives compile time error
System.out.println("child");
}
}
class Test1{
public static void main(String[] args) {
Parent p = new Parent();
p.msg();
Parent c = new Child();
c.msg();
Child d = new Child();
d.msg();
Child2 e = new Child2();
e.msg();
}
}
请解释我在某处是否错了。