jim
jim

Reputation: 9138

Handling exceptions with interfaces

So, I am writing some interfaces for connectivity. Example, one interface is defined that can be implemented using Sockets, Serial IO or other.

Both socket and serial connectivity have different exceptions.

Usually, when I am implementing something I have the methods declare the exceptions using throws.

It is a problem in the case of interfaces however. Should the interface declare that both Socket related and Serial etc related exceptions are thrown and handle them all, all of the time?

What should I do?

Thank you.

Upvotes: 0

Views: 532

Answers (3)

Laf
Laf

Reputation: 8215

Generally speaking, whenever you are defining an interface, the methods you provide should only throw the exception that makes sense in the context your API will be used. If users of your API are not at a level where handling very specific exceptions (the socket or serial exceptions in your case), then indeed as others have correctly stated, wrapping them in a "broader" exception is your best solution.

Keep in mind that adding throws clauses in your interface definition has some impact in either your code or your clients' code.

Given the following interface:

public interface MyInterface {
    void doSomething () throws IOException;
}

Users of your API will be forced to surround calls to doSomething () with a try/catch (or throw the exception to the calling method) if they are calling the method via the interface. E.g.:

public class MyImplementation implements MyInterface {
    @Override
    public void doSomething () {
        /*
         * It is legal to override a method defined in the interface and not throw
         * the exception(s) that were defined in the interface definition.
         */
    }
}

public class OtherClass {
    public static void main (String[] args) {
        MyInterface object = new MyImplementation ();
        object.doSomething (); // Compilation error here since IOException is not handled.
    }
}

Always remember that while defining your interface, you are setting up a contract that implementing classes and your API's users will have to live with. So make sure that whatever you define in your interface is necessary, and not a case of "what-if-I-ever-need-this".

Upvotes: 0

RamanSB
RamanSB

Reputation: 1172

Interface methods are by default public abstract and can throw anything that is a subclass of the throwable (included) you wish, however when implementing the method, the method signature should either throw the same Exception, a subclass of that exception or none.

In your case, your interface would look something like this, along with any other methods (default/static or abstract) you may have.

interface network{
    void connect() throws IOException;
}

Upvotes: 0

Fernando Miguélez
Fernando Miguélez

Reputation: 11316

Just define the interface throwing an IOException the same way as stream api does. You can then in your implementation define extended types from IOException with specific details (socket disconnections, etc.).

Upvotes: 2

Related Questions