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?