java - 在重写方法中检查'throws'的异常 [英] java - checked exception for 'throws' in overridden method

查看:160
本文介绍了java - 在重写方法中检查'throws'的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java中的方法覆盖来练习异常处理机制...我的代码如下:

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

显示错误:

所以,我更正了以下内容:

So, I corrected following:

void show() throws IOException{

它工作正常......

and it is working correctly...

我做了另一个实验:

void show() throws Exception{

但它也显示错误:

据我所知,这是因为重写方法的抛出子句应该在超类方法的 throws 子句中提到精确检查的异常。

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.

与第二种情况一样,如果我在throws子句中写 IOException 的超类异常,它也会显示错误。为什么?即使 Exception 是所有例外的父类。

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?

推荐答案

样本中有两个相关错误:

There are two related errors in the sample:

1)
您的基类方法为<模板基本条件提供派生类方法。

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 {
        //...
    }
}

然后基类方法必须为:

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

所以这两项都有效:

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

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

2)当您尝试上述操作时, show 方法的整体声明现在变为抛出异常。因此,使用此 show 的任何人都必须捕获该异常。

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.

中main 方法,您正在捕获 IOException 。这将不再有效,编译器抱怨你好,你正在捕获IOException,那么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.

要解决此问题,请更改 main 方法catch以包含基类中声明的异常

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

这篇关于java - 在重写方法中检查'throws'的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆