Wednesday, September 3, 2014

TooManyExceptionsException

Some Java programmers feel the need to write custom exceptions for each case scenario where a failure might happen. But to do this, and handle these failures in the same manner kinda contradicts the purpose of the whole thing.

try {
    fooService.handleFoo(foo, bar);
} catch (FooCreationException e) {
    log.add(e.getMessage());
} catch (FooNotFoundException e) {
    log.add(e.getMessage());
} catch (ManagerException e) {
    log.add(e.getMessage());
} catch (Exception e) {
    log.add(e.getMessage());
}

Here the exception type is lost and only the error message is kept.