总是在接口方法上声明“抛出异常”? [重复]

时间:2021-08-11 21:29:29

This question already has an answer here:

这个问题在这里已有答案:

If you have an interface and you don't know how its methods are going to be implemented (i.e. it's a library which other users will extend), how do you know if implementations of that method will be exception-prone? Should you just declare "throws Exception" on every interface method because you don't know the implementation?

如果你有一个接口而你不知道它的方法将如何实现(即它是一个其他用户将扩展的库),你怎么知道该方法的实现是否会异常?您是否应该在每个接口方法上声明“抛出异常”,因为您不知道实现?

For example, I'm making an http client. This library has an Request interface that represents an http request string:

例如,我正在制作一个http客户端。该库有一个Request接口,表示一个http请求字符串:

public interface Request
{
    String asString(); // no "throws" here  
}

public final class AssembledRequest implements Request
{
    // ...
    public AssembledRequest(Method method, Url url, Headers headers, Body body) {
        // ...
    }
    public String asString() {
        // ...
    }
}

It also has a Response class, which uses this Request interface. My own implementation (AssembledRequest) doesn't throw exceptions, it just assembles a bunch of strings and probably most implementations won't throw anything as well.

它还有一个Response类,它使用这个Request接口。我自己的实现(AssembledRequest)不会抛出异常,它只是组装了一堆字符串,可能大多数实现都不会抛出任何东西。

But what if a user wants to implement Request in a way that asString reads from a file? Then we're dealing with IOExcepton and it will be very difficult for a user to handle such exceptions. No throws... declared, so you'd have to handle it inside the method, which is a no no, since you want to catch it and rethrow, so it would bubble up to the top of your application and then be handled.

但是如果用户想要以asString从文件中读取的方式实现Request呢?然后我们处理IOExcepton,用户很难处理这些异常。没有抛出...声明,所以你必须在方法内部处理它,这是不可能的,因为你想捕获它并重新抛出,所以它会冒泡到应用程序的顶部然后被处理。

If Request interface had a throws Exception declared on asString it would be much easier. Which makes me conclude that for libraries, all interfaces' methods should have throws Exception. Am I right?

如果Request接口在asString上声明了抛出异常,则会更容易。这让我得出结论,对于库,所有接口的方法都应该抛出异常。我对吗?

1 个解决方案

#1


-1  

The interface is the API between implementation and specification. If you're authoring the API, then you would know if/when your API would allow something to be thrown.

接口是实现和规范之间的API。如果您正在编写API,那么您将知道您的API是否/何时允许抛出某些内容。

That said, I would take one of these two actions:

那就是说,我会采取以下两种行动之一:

  • Implement a separate interface where throwing these sorts of exceptions is acceptable, or
  • 实现一个单独的接口,可以接受抛出这些异常,或者

  • Wrap the checked exception in a RuntimeException of some sort and let it bubble up anyway.
  • 将检查过的异常包装在某种RuntimeException中,然后让它冒泡。

Exceptions are exceptional for a reason. Unless your code explicitly expects it, don't explicitly allow for it.

出于某种原因,例外是例外情况。除非您的代码明确指望它,否则不要明确允许它。

#1


-1  

The interface is the API between implementation and specification. If you're authoring the API, then you would know if/when your API would allow something to be thrown.

接口是实现和规范之间的API。如果您正在编写API,那么您将知道您的API是否/何时允许抛出某些内容。

That said, I would take one of these two actions:

那就是说,我会采取以下两种行动之一:

  • Implement a separate interface where throwing these sorts of exceptions is acceptable, or
  • 实现一个单独的接口,可以接受抛出这些异常,或者

  • Wrap the checked exception in a RuntimeException of some sort and let it bubble up anyway.
  • 将检查过的异常包装在某种RuntimeException中,然后让它冒泡。

Exceptions are exceptional for a reason. Unless your code explicitly expects it, don't explicitly allow for it.

出于某种原因,例外是例外情况。除非您的代码明确指望它,否则不要明确允许它。