Let's say I have the following Java interface that I may not modify:
public interface MyInterface {
public void doSomething();
}
And now the class implementing it is like this:
class MyImplementation implements MyInterface {
public void doSomething() {
try {
// read file
} catch (IOException e) {
// what to do?
}
}
}
I can't recover from not reading the file.
A subclass of RuntimeException
can clearly help me, but I'm not sure if it's the right thing to do: the problem is that that exception would then not be documented in the class and a user of the class would possibly get that exception an know nothing about solving this.
What can I do?
We all agree: the interface is faulty.
Solution I chose
I finally decided to write a MyVeryOwnInterface
that extends MyInterface
and adds as part of the signature of the faulty methods the MyRuntimeException
:
public interface MyVeryOwnInterface extends MyInterface {
public void doSomething() throws MyRuntimeException;
}
class MyImplementation implements MyVeryOwnInterface {
public void doSomething() throws MyRuntimeException {
try {
// read file
} catch (IOException e) {
throw new MyRuntimeException("Could not read the file", e);
}
}
}