-1

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:

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.

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.

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?

Justas B.
  • 67
  • 1
  • 7
  • You should know if the methods have the potential to throw an exception. If you don't want to have it throw an exception then you will have to `try-catch` in the methods that implement it if they are exception prone. – Rabbit Guy Jul 26 '17 at 19:04
  • Adding `throws Exception` to an interface method because someday, maybe, just perhaps, someone out there might need it is the worst kind of YAGNI (You aren't gonna need it) there is. You make all future generations pay forever. Not a good plan. – Bob Dalgleish Jul 26 '17 at 19:06
  • @BobDalgleish How would you solve my problem when `asString` reads from a file? – Justas B. Jul 26 '17 at 19:14
  • Implementations can always throw an _unchecked_ exception, `RuntimeException`, if they face something they cannot handle. But the general contract (interface JavaDoc) should specify what kind of error handling is expected. – Mick Mnemonic Jul 26 '17 at 19:17

1 Answers1

-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.

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.

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

Makoto
  • 104,088
  • 27
  • 192
  • 230