java - checked exception for 'throws' in overridden method

ぃ、小莉子 提交于 2019-12-01 12:09:42
metacubed

There are two related errors in the sample:

1) Your base class method provides the "template" or basic criteria for the derived class method.

So, the base class should declare a super-set i.e. either the same exception class or base exception class of the derived class. You cannot declare that it throws nothing, because then the criteria will not match.

So if your derived class method is like this:

class Derived extends Base {
    void show() throws IOException {
        //...
    }
}

Then base class method "must" be:

class Base {
    void show() throws /*Same or base classes of IOException*/ {
        //...
    }
}

So both of these work:

class Base {
    void show() throws Exception {
        //...
    }
}

or

class Base {
    void show() throws Throwable {
        //...
    }
}

2) When you try the above, the overall declaration of your show method now becomes throws Exception. As a result, anyone who uses this show must catch that exception.

In your main method, you are catching IOException. This will no longer work, the compiler complains "ok you are catching the IOException, what about all the other possibilities from Exception?" This is the second error that you showed.

To fix this, change the main method catch to include Exception as declared in the base class:

class My {
    public static void main(String[] args) {
        try {
            base b = new derived();
            b.show();
        }
        /* NOTE: CHANGED FROM IOException TO Exception */
        catch (Exception e) {
            System.out.println("exception occurred at :" + e);
        }   
    }
}

Overridden methods may contain only the same checked exceptions in their throws clause as the super-method, or derived types at most.

For example if you say

class Base {

    public void foo(int y) throws IOException {
        // ...
    }
}

and

class Derived extends Base {

    public void foo(int y) throws Exception {
        // ...
    }
}

then your compiler will say that the foo method inside Derived is incompatible with the throws clause in its superclass.

The other way around works because if I say

class Base {

    public void foo(int y) throws Exception {
        // ...
    }
}

and

class Derived extends Base {

    public void foo(int y) throws IOException {
        // ...
    }
}

it's OK.

Why.

Think about the usage of your methods. Java expects you to be using the method polymorphically such as

Base a = new Derived();
a.foo(3);

As such, the compiler will force you to catch the exception thrown by foo in your declared type of the variable (Base). So your code will become

Base a = new Derived();
try {
    a.foo(3);
} catch (Exception e) {
    // ...
}

Therefore, the subtype of Exception you declared in the Derived type is OK with your code above (a catch for an Exception will work for any of its subtypes as well) and as such, Java will allow you to declare IOException in derived, because it will cause no worries later on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!