In applications where the accepted practice is to log an Exception
and then rethrow it, you end up with miles-long logs that contain
multiple instances of the same exception. In multi-threaded applications debugging this type of log can be particularly hellish because messages from
other threads will be interwoven with the repetitions of the logged-and-thrown Exception
. Instead, exceptions should be either logged or
rethrown, not both.
Noncompliant code example
catch (SQLException e) {
...
LOGGER.log(Level.ERROR, contextInfo, e);
throw new MySQLException(contextInfo, e);
}
Compliant solution
catch (SQLException e) {
...
throw new MySQLException(contextInfo, e);
}
or
catch (SQLException e) {
...
LOGGER.log(Level.ERROR, contextInfo, e);
// handle exception...
}