After reading Why can't overriding methods throw exceptions, I understand that if method declared as throws a Checked exception, the overriding method in a subclass can only declare to throw that exception or its subclass:
class A {
public void foo() throws IOException {..}
}
class B extends A {
@Override
public void foo() throws SocketException {..} // allowed
@Override
public void foo() throws SQLException {..} // NOT allowed
}
So because SocketException
IS-A IOException
I can declare the overriding method as throws any of subclass of IOException
.
In my program, I want to invoke the overriding method declared as throws FileNotFoundException
IS-A IOException
. also handled with a try-catch block
import java.io.*;
class Sub extends Super{
public static void main (String [] args){
Super p = new Sub();
try {
p.doStuff();
}catch(FileNotFoundException e){
}
}
public void doStuff() throws FileNotFoundException{}
}
class Super{
public void doStuff() throws IOException{}
}
But I am getting that compile-time error:
Sub.java:6: error: unreported exception IOException; must be caught or declared to be thrown
p.doStuff();
^
What is the reason for that? I'm a little confused because everything that the Base class has also available to the subclasses.
Also much more confusing is the ability to catch Exception
and Throwable
In addition to IOException
(The opposite from Overriding concept).
An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
No, you cannot. The sub-class method cannot throw any checked exception not covered by the throws clause of the base super class method.
Overridden methods can throw Exceptions, so long as the method being overridden also throws the same Exceptions. You can't introduce new Exceptions.
Your object reference is of type Super
, even though you know it's a Sub
object at runtime. Therefore the compiler is checking the method definition of the Super
and giving you this compile error.
It would be no different from getting the following compiler error:
Object o = new String("Hello World");
o.charAt(2); //Obviously not allowed
It's important to remember that the throws
clause is part of the method definition.
What is the reason for that? I'm a little confused because everything that the Base class has also available to the subclasses.
You need to catch an IOException
and not a FilenotFoundException
. This is because of the fact that while the doStuff
method from the subclass will be called at runtime, the compiler doesn't know about this yet. It only knows about the doStuff
method in the super class which declares that it throws
an IOException
.
To address your edit : The catch
block can chose to catch the exact exception that is expected in the try
block or it can chose to catch a superclass of the exception. The reasoning behind this has got nothing remotely to do with method overriding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With