I am developing a library for working with files and want to make it as easy to use (i.e. not having to worry about exceptions), but also as complete as possible (i.e. allow for proper exception handling).
To achieve this, I expose two methods. One that allows for normal catching of exceptions:
public void saveChecked() throws IOException, IllegalArgumentException {
// Possibly throw an IOException or IllegalArgumentException
}
...and one that handles all exceptions using a generic Consumer<Exception>
:
public void save(Consumer<Exception> handler) {
try {
saveChecked();
} catch (Exception exception) {
handler.accept(exception);
}
}
The intention behind this is, that, if you don't need to differentiate between exceptions and just want to do one simple thing when an exception is thrown (e.g. print the stack trace, show a popup), you can just use the second version instead of having to write out a try-catch block (and making one-liners impossible).
Is this still bad practice even though the API does expose a method which allows proper exception handling for those that need it?