【问题标题】:java - checked exception for 'throws' in overridden methodjava - 在覆盖方法中检查“抛出”异常
【发布时间】:2014-08-09 15:55:55
【问题描述】:

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

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

显示错误:

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

void show() throws IOException{

它工作正常......

我又做了一个实验:

void show() throws Exception{

但它也显示错误:

据我了解,这是因为重写方法的 throws 子句应在超类方法的 throws 子句中提及确切的检查异常。

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

我只是做了实验...这个错误告诉我什么我不知道...

谁能解释一下它所说的内容以及在覆盖方法的throws 子句中提及检查异常的限制条件是什么?

【问题讨论】:

  • 我误解了你的问题。忽略你在我的回答中读到的内容。
  • @SotiriosDelimanolis 好的...没问题..但是如果我只在派生中声明 Exception,你能说我,我可以在超类中声明 IOException 吗?请回答,不要发表评论。

标签: java exception inheritance overriding throws


【解决方案1】:

样本中有两个相关错误:

1) 您的基类方法为派生类方法提供“模板”或基本标准。

因此,基类应该声明一个超集,即与派生类相同的异常类或基异常类。您不能声明它什么都不抛出,因为那样条件将不匹配。

所以如果你的派生类方法是这样的:

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 方法的整体声明现在变为throws Exception。因此,任何使用此show 的人都必须捕获该异常。

在您的main 方法中,您正在捕获IOException。这将不再起作用,编译器抱怨“好吧,你正在捕获 IOException,那么来自 Exception 的所有其他可能性呢?”这是您显示的第二个错误。

要解决此问题,请将 main 方法 catch 更改为包含基类中声明的 Exception:

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

【讨论】:

  • @krupalshah 不,因为基类说“允许任何 IOException”被抛出。所以派生类只能抛出某种 IOException 来匹配该限制。
  • 否,派生类只能声明相同的 OR 子异常或运行时异常。
  • 我知道,如果你想更好地了解这个简单的例子。 code.google.com/p/learn-java/wiki/OverridenMethod
  • 那是不可能的...我会把类似的代码粘贴到答案中
  • @krupalshah 这完全是另一个错误。它来自您的main 函数捕获。因为基类现在抛出异常,所以您还必须捕获异常,而不是 IOException。
【解决方案2】:

被覆盖的方法只能在其throws 子句中包含与超方法或派生类型相同的检查异常。

例如,如果你说

class Base {

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

和

class Derived extends Base {

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

那么你的编译器会说Derived 中的foo 方法与其超类中的 throws 子句不兼容。

反之亦然,因为如果我说

class Base {

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

和

class Derived extends Base {

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

没关系。

为什么。

想想你的方法的使用。 Java 期望您以多态方式使用该方法,例如

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

因此,编译器将强制您在声明的变量类型 (Base) 中捕获 foo 引发的异常。所以你的代码会变成

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

因此,您在Derived 类型中声明的Exception 的子类型 可以使用上面的代码(Exception 的捕获也适用于它的任何子类型)因此,Java 将允许您在派生中声明 IOException,因为以后不会有任何后顾之忧。

【讨论】:

  • 告诉我,如果我只在派生中声明异常,我可以在超类中声明 IOException 吗?
  • 这就是重点。不,你不能。如果你反过来说,你在 base 中声明 Exception,在派生中声明 IOException,那么这是可能的。
  • 你能更好地解释一下为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
相关资源
最近更新 更多