Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking overriding method that throws Checked exception

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: Screenshot

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).

like image 650
Pauwelyn Avatar asked Jan 24 '17 11:01

Pauwelyn


People also ask

Can we throw checked exception from overridden method?

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.

Is it possible to override a super class method that throws an unchecked exception with checked exceptions in the sub class?

No, you cannot. The sub-class method cannot throw any checked exception not covered by the throws clause of the base super class method.

Can we override methods with same signature but in child class it is throwing exception?

Overridden methods can throw Exceptions, so long as the method being overridden also throws the same Exceptions. You can't introduce new Exceptions.


2 Answers

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.

like image 81
StuPointerException Avatar answered Sep 22 '22 08:09

StuPointerException


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.

like image 43
Chetan Kinger Avatar answered Sep 18 '22 08:09

Chetan Kinger