When you implement an interface method in your class, you are not required to provide the same exception arguments. The same case applies when you override a method declaration from a superclass.
public class MyReader implements Readable { @Override public int read(CharBuffer cb) { return 0; } }
But then you are not using the interface correctly. And it is not profitable for you if you code the interface.
Readable readable = new MyReader(); try { readable.read(null); } catch (IOException e) { e.printStackTrace(); }
Even in MyReader do not MyReader IOException , you still have to use a try block. Therefore, if you did not select an exception from the method that you are implementing, it may indicate that you missed something in your implementation of this method. Therefore, IMHO this is not a good practice.
The reason StringBuilder throws an IOException is not because it implements the Readable interface. The reason for this is to check the input in the ensureOpen() method, which throws an IOException when the input is null. Then the input can be null when the close() method is called or you pass null to the constructor. Since the close method is abstract, it should have some effect in the child class. It is expected that after you call, you will no longer be able to read it, and you will receive an IOException.
This is an ideal, clean and reliable implementation that takes into account all possible use cases.
source share