class Foo
{
void process() throws Exception{
System.out.println("Foo");
throw new Exception();
}
}
class Bar extends Foo{
void process(){
System.out.println("Bar");
}
public static void main(String[] args)
{
new Bar().process();
}
}
why does compiler not complain that i dont treat the exception thrown in class Foo ? Why do i not have to wrap the call to new Bar().process() in a try/Catch block ?
Remember that compiler takes into account only the type of the reference (and not the type of the actual object pointed to by that reference) to determine whether the method call made using that reference may potentially throw an exception or not. Here the type of reference on which you are invoking process() is Bar. And Bar overrides process() without any throws clause. So the compiler knows that Bar's process does not throw any exception and therefore there is no need for try/catch.