java 抛出接口中未定义的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Throwing an Exception Not Defined in the Interface
提问by Scott
What is the best practice to follow when you need to throw an exception which was not defined in an interface that you are implementing?
当您需要抛出未在您正在实现的接口中定义的异常时,应遵循的最佳实践是什么?
Here is an example:
下面是一个例子:
public interface Reader
{
public abstract void read() throws IOException;
}
public class CarrotReader implements Reader
{
public void read() throws IOException {}
}
public class CupcakeReader implements Reader
{
public void read() throws IOException, CupcakeException {}
}
In this case, you have a specific exception that occurs when reading cupcakes, so you want to throw an exception related to this. However, Readerdoesn't define this type of exception in its interface, so what do you do? Furthermore, it doesn't make sense to add CupcakeExceptionto the throwsclause in the Readerinterface, because this type of exception is specific to CupcakeReader. One way around this is to have Readerdefine readsuch that it throws some parent type, like Exception, but then you lose the context for the exception. What should you do in this situation? Thanks!
在这种情况下,您在读取纸杯蛋糕时发生了一个特定的异常,因此您想抛出一个与此相关的异常。但是Reader并没有在它的接口中定义这种异常,那怎么办呢?此外,将CupcakeException添加到Reader接口中的throws子句是没有意义的,因为这种类型的异常特定于CupcakeReader。解决此问题的一种方法是让Reader定义read以使其抛出一些父类型,例如Exception,但随后您会丢失异常的上下文。在这种情况下你应该怎么做?谢谢!
Another interesting situation that has been brought up involves an interface over which you have no control. In this case, what is the best way to indicate that a problem has occurred?
提出的另一个有趣的情况涉及您无法控制的界面。在这种情况下,表明出现问题的最佳方式是什么?
For illustrative purposes, here is another example:
出于说明目的,这是另一个示例:
public interface Reader
{
public abstract void read();
}
public class CupcakeReader implements Reader
{
public void read() throws CupcakeException {}
}
In this case, you cannot change Reader, but you want to indicate that a problem has occurred in CupcakeReader's readmethod.
在这种情况下,您无法更改Reader,但您想表明CupcakeReader的read方法出现问题。
采纳答案by Thorbj?rn Ravn Andersen
You may have to create an exception of the expected type instead.
您可能必须改为创建预期类型的异常。
... catch(CupcakeException e) {
throw new IOException("The sky is falling", e);
}
回答by Srikar Doddi
Use something called ReaderException that will serve as the root interface of your exception hierarchy. ReaderException will also provides a link to other exceptions that get thrown due to lower level exceptions.
使用称为 ReaderException 的东西作为异常层次结构的根接口。ReaderException 还将提供指向由于较低级别异常而引发的其他异常的链接。
回答by ZZ Coder
Exception is part of the interface. Define a generic parent for all your exceptions in the interface if you can redefine the interface.
异常是接口的一部分。如果您可以重新定义接口,请为接口中的所有异常定义一个通用父级。
You can also make CupcakeException a child of IOException.
您还可以使 CupcakeException 成为 IOException 的子项。
回答by Gregory Mostizky
Just don't use checked exceptions.
只是不要使用已检查的异常。
The example you showed is one of the reasons checked exceptions are bad.
你展示的例子是检查异常不好的原因之一。
The main reason though is that the user of your cupcake reader will have to handle your exception regardless of whether he is interested in it or not.
不过,主要原因是您的纸杯蛋糕阅读器的用户必须处理您的异常,无论他是否对它感兴趣。
So instead of:
所以而不是:
Value value = reader.read();
You are forcing him to do this:
你强迫他这样做:
Value value = null;
try {
value = reader.read();
} catch (Exception e) {
// now what??
}
value.doSomething(); // potential NPE here
Think which one is better, more readable and less error prone and just stop using checked exceptions.
想想哪一个更好、更易读、更不容易出错,然后停止使用检查异常。
EDIT:
编辑:
I am surprised with the negative rating. Are there people who still think that checked exceptions are great? If so here are some references why you shouldn't use checked exceptions:
我对负面评价感到惊讶。是否还有人认为受检异常很棒?如果是这样,这里有一些参考为什么你不应该使用检查异常:
回答by Janusz
If you create a higher abstract exception that works as a base class for CupCakeException you don't bind the Reader Interface to a specific implementation like you would be doing if you added the CupCakeException to the Reader interface. If you don't let one Exception inherit from another there is a constructorin the exception class that takes a throwable as second argument like Thorbj?rn Ravn Andersen allready showed in his short code example. The enables you to generate a more abstract exception and every part of your code that needs to know more then just "there is an error" can look for the cause of the higher exception.
如果您创建一个更高的抽象异常作为 CupCakeException 的基类,您不会像将 CupCakeException 添加到 Reader 接口时那样将 Reader Interface 绑定到特定实现。如果你不让一个 Exception 继承另一个 Exception异常类中有一个构造函数,它接受一个 throwable 作为第二个参数,就像 Thorbj?rn Ravn Andersen 已经在他的简短代码示例中展示的那样。这使您能够生成更抽象的异常,并且需要了解更多信息的代码的每个部分然后只是“存在错误”可以查找更高异常的原因。
回答by Zed
Perhaps you could make an abstract ReaderSpecificException class, put it in the Interface, and subclass CupcakeException from this abstract class.
也许你可以创建一个抽象的 ReaderSpecificException 类,把它放在接口中,然后从这个抽象类中继承 CupcakeException。

