我有一个子类,我重写继承的父方法:我从方法声明中删除throws子句.
现在,使用多态,我的实例的运行时类型应该确定方法实现; 然而,当我尝试编译时,编译器抱怨并想要一个围绕方法调用的try/catch块,就像调用超类方法而不是子类版本一样?
我知道我可以删除throws声明或缩小已检查的异常,这可以在覆盖时抛出.为什么这仍然抛出异常?
class A{
void foo()throws Exception{throw new Exception();}
}
class SubB extends A{
@Override
void foo(){ System.out.println("B");
}
public static void main(String[] args) {
A a = new SubB();
a.foo(); //compiler reports: unhandled exception type Exception
}
Run Code Online (Sandbox Code Playgroud) java ×1