简体   繁体   English

未检查与检查异常的接口声明

[英]Interface Declaration with Unchecked vs. Checked Exceptions

I've searched for a potential answer to my question below, and have not found one.我在下面搜索了我的问题的潜在答案,但没有找到。

I understand the difference between checked and unchecked exceptions as well as what the programmer can/must do with them.我了解已检查和未检查异常之间的区别以及程序员可以/必须对它们做什么。 However, I don't quite understand how the compiler interprets unchecked exceptions when it comes to interfaces.但是,我不太了解编译器在涉及接口时如何解释未经检查的异常。

Recently, I was programming an interface and implementation similar to the following:最近,我正在编写一个类似于以下的接口和实现:

public interface Operation {
    int operate(int x, int y) throws ArithmeticException;
}

public class Divide implements Operation {
    @Override
    public int operate(int x, int y) throws ArithmeticException {
        try {
            return x / y;
        } catch (ArithmeticException ex) {
            System.out.println("Division error!");
        }
    }
}

Here's where I'm confused.这就是我感到困惑的地方。 The following implementation will also compile:以下实现也将编译:

public class Divide implements Operation {
    @Override
    public int operate(int x, int y) {
        return x / y;
    }
}

Why does the compiler not care that I have declared the method as throwing an exception in the interface?为什么编译器不关心我已将方法声明为在接口中引发异常? I understand that Unchecked Exceptions are Runtime Exceptions and that it is not required that the programmer handle them, but do not understand why when I explicitly state in my interface that I want the exception handled that it does not force me to handle it.我知道未经检查的异常是运行时异常,程序员不需要处理它们,但不明白为什么当我在我的界面中明确地 state 我希望处理异常时它不会强迫我处理它。

Could someone lend an explanation as to why the compiler will allow this situation to occur?有人可以解释为什么编译器会允许这种情况发生吗?

You're confusing "handling" an exception with inheritance rules.您将“处理”异常与 inheritance 规则混淆了。 When you implement or override a method, there is no rule that says the new method must throw exactly the same exceptions that the implemented/overridden method does.当你实现或重写一个方法时,没有规则说新方法必须抛出与实现/重写的方法完全相同的异常。 The only rule is that it cannot throw a checked exception that is outside the range of allowable exceptions for the parent method.唯一的规则是它不能抛出超出父方法允许异常范围的已检查异常。 Think of the interface as establishing a contract with the outside world.将接口视为与外部世界建立合同。 It says, "anybody who calls this method must be prepared to handle these types of exception".它说,“任何调用此方法的人都必须准备好处理这些类型的异常”。 Implementors are then free to throw the exception, knowing that it will be handled, but they're by no means required to throw it.然后,实现者可以自由地抛出异常,知道它将被处理,但他们绝不需要抛出它。

In addition to the previous answers, its worth to note that this code is perfectly ok for checked exceptions:除了前面的答案,值得注意的是,这段代码对于检查异常来说是完全可以的:

interface Translator {
    String translate(String s) throws IOException; /* <-- Checked exception */
}

class EnglishGermanTranslator implements Translator {
    @Override
    public String translate(String s) {  // <-- no exception, but its ok
        return ""; // logic is irrelevant...
    }
}

public class Test {
    public static void main(String[] args) {
        EnglishGermanTranslator t = new EnglishGermanTranslator();
        t.translate("Text to translate");  // No problems...
    }
}

But when we refer to the EnglishGermanTranslator via Translator , we have to catch an appropriate exception:但是当我们通过Translator引用EnglishGermanTranslator时,我们必须捕获一个适当的异常:

public class Test {
    public static void main(String[] args) {
        Translator t = new EnglishGermanTranslator();
        t.translate("Text to translate");  // <-- compilation error... try-catch required  (or throws in main)
    }
}

Declaring runtime exceptions in the throws clause is only for the sake of information (and javadoc) - so that the users of your class/interface know what to expect in a given situation.throws子句中声明运行时异常只是为了提供信息(和 javadoc) - 以便您的类/接口的用户知道在给定情况下会发生什么。

Since they are runtime, the compiler does not enforce anything about them - neither catching, nor declaring.由于它们是运行时的,因此编译器不会对它们强制执行任何操作——既不捕获也不声明。

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

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