Is it an anti-pattern to handle exception in a separate method?
Say i have a method that does some low-level IO and may throw an IOException, and i have a method foo() that calls the low-level IO method several times. Does it make sense to do the exception-handling in a 3rd method like this:
public void foo() throws MyCheckedException {
// some stuff
goDoSomeIO(path1)
// some other stuff
goDoSomeIO(path2)
// some more stuff
goDoSomeIO(path3)
}
private String goDoSomeIO(String filePath) throws MyCheckedException {
try {
doSomeIO(filePath);
} catch (IOException ioe) {
LOG.error("Io failed at: " + filePath);
throw new MyCheckedException("Process failed because io failed", ioe)
}
}
private String doSomeIO(String filepath) throws IOException {
//io stuff
}
I find this is more readable than it would be if the doSomeIO method did its own exception handling, or if the exception handling happend in foo.