Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

java - checked exception for 'throws' in overridden method

I was practicing exception handling mechanisms with method overriding in java...My code is as follows:

class base {
    void show() {    
        System.out.println("in base class method");    
    }
}

class derived extends base {
    void show() throws IOException {    
        System.out.println("in derived class method");
        throw new IOException();
    }
}

class my {
    public static void main(String[] args) {  
        try {
            base b = new derived();
            b.show();
        }    
        catch (IOException e) {
            System.out.println("exception occurred at :" + e);
        }   
    }
}

Showing an error :

cmd window

So, I corrected following:

void show() throws IOException{

and it is working correctly...

One more experiment I did :

void show() throws Exception{

but it was also showing an error:

cmd window

As I understand, this is because of an overridden method's throws clause should mention the exact checked exception in throws clause of super class method.

As in second case, if I write IOException's superclass Exception in throws clause, it also shows an error. why? Even if Exception is parent class of all exceptions.

I just experimented...what this error tells I don't know...

Can any one please explain what it says and what are the constraints for mentioning checked exception in throws clause of an overridden method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }   
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...