I have a subclass and I am overriding the inherited parent method: I am removing the throws clause from the method declaration.
Now, using polymorphism, the my instance’s runtime type should determine the method implementation; yet, the compiler complains and wants a try/catch block around the method invocation when I try to compile, as if the superclass method is being invoked and not the subclass version?
I’m aware that I can remove the throws declaration or narrow down the checked exception which can be thrown when overriding. Why is this still throwing the exception?
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
}
The compiler ses an
Awhich does throw an Exception. If you however told the compiler it’s an actualSubBobject it will stop complaining