简体   繁体   English

使用新检查的异常覆盖Java接口

[英]Overriding Java interfaces with new checked exceptions

Suppose in Java, I'm using a preexisting interface that is rather general 假设在Java中,我使用的是一个相当普遍的预先存在的接口

public interface Generator {
    public String generate();
}

and I have my own class 我有自己的课

public class FromFileGenerator implements Generator {
    ...
    public String generate() throws FileNotFoundException {
        String output = //read from some file
        return file;
    }
}

The Java compiler yells at me because the implementation of generate() contains an exception not specified in the original signature (FileNotFoundException). Java编译器对我大吼大叫,因为generate()的实现包含未在原始签名中指定的异常(FileNotFoundException)。 However, clearly the exception does not belong in the interface, but it also cannot be neglected in the implementing class. 但是,显然异常不属于接口,但在实现类中也不能忽略。 How can this be resolved without simply failing silently? 如果不是默默地失败,怎么能解决这个问题呢?

You cannot add a checked exception to the declaration of a method that hides, overrides, or implements another method. 您不能将已检查的异常添加到隐藏,覆盖或实现其他方法的方法的声明中。 You will need to catch the exception within the method and do something else with it (such as return null , or throw an unchecked exception, possibly wrapping the checked exception). 您将需要捕获方法中的异常并对其执行其他操作(例如返回null ,或抛出未经检查的异常,可能包装已检查的异常)。

From the Java Language Specification, §8.4.8.3 : Java语言规范,§8.4.8.3

A method that overrides or hides another method, including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method. 覆盖或隐藏另一个方法的方法(包括实现接口中定义的抽象方法的方法)可能不会被声明为抛出比重写或隐藏方法更多的已检查异常。

Unfortunately, there is no way to resolve it nicely: exception specifications are part of the interface, so if someone who programs to your Generator interface is not expecting to see FileNotFoundException , it shouldn't be coming to them when they use FromFileGenerator . 不幸的是,没有办法很好地解决它:异常规范是接口的一部分,所以如果编程到您的Generator接口的人不希望看到FileNotFoundException ,那么当他们使用FromFileGenerator时不应该找到它们。

A common way to resolve it is by introducing a common exception, and wrapping FileNotFoundException in it: 解决此问题的常用方法是引入一个常见异常,并在其中包装FileNotFoundException

public class GeneratorException extends Exception {
    public GeneratorException(Exception inner) {
        super(inner);
    }
}
public interface Generator {
    public String generate() throws GeneratorException;
}
public class FromFileGenerator implements Generator {
...
    public String generate() throws GeneratorException {
        try {
            String output = //read from some file
            return file;
        } catch (FileNotFoundException fnf) {
            throw new GeneratorException(fnf);
        }
    }
}

Interface is a contract . 接口是合同

在此输入图像描述

So you need to define that upfront. 所以你需要预先定义。

So you can either do a try, catch within your method body or modify the contract. 因此,您可以尝试,在方法体内捕获或修改合同。

You can wrap the implementation exception in an unchecked exception and throw that: 您可以将实现异常包装在未经检查的异常中并抛出:

public class FromFileGenerator implements Generator {
    ...
    public String generate() {
        try {
            String output = //read from some file
            return file;
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
}

A common solution to this is to wrap the exception in an unchecked exception. 对此的常见解决方案是将异常包装在未经检查的异常中。 So your class might look something like: 所以你的课可能看起来像:

public class FromFileGenerator implements Generator {
    ...
    public String generate() throws FileNotFoundException {
        try {
            String output = //read from some file
            return output
        } catch (FileNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}

Even better would be to change the interface to have its own checked exception. 更好的方法是将接口更改为具有自己的已检查异常。 Then you could convert from FileNotFoundException to that checked exception in the same way that you convert to the unchecked IllegalStateException above. 然后,您可以将FileNotFoundException转换为该已检查的异常,方法与转换为上面未经检查的IllegalStateException的方式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM