Luke Davis
Luke Davis

Reputation: 163

Is it okay to not implement exception throws in an interface implementation?

I've made a more specific ListIterator for singly linked lists and I have implemented most of the methods. I see in the description that some methods should throw exceptions but some exceptions don't seem particularly relevant, especially as I'm using generics. Is it fine to not have these exceptions in the implementation?

Upvotes: 4

Views: 213

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075019

Fundamentally, since the throws isn't part of the method signature, if your implementation of a method doesn't throw the relevant exception, you can leave that exception off.

E.g., this is perfectly valid:

public interface MyInterface {
    void method() throws Exception;
}

and

public class Example implements MyInterface {
    public void method() {
    }
}

Upvotes: 4

Related Questions