Ir al contenido

Log And Rethrow Duplication (ARCH4166)

Esta página aún no está disponible en tu idioma.

Key java.logging.log-and-rethrow · Severity warning · Category logging

Both logging and rethrowing an exception produces duplicate entries because it will be logged again higher up the stack. Either log or throw the exception, not both.

try {
repository.save(user);
} catch (SQLException e) {
log.error("Save failed", e);
throw new DataAccessException(e);
}
try {
repository.save(user);
} catch (SQLException e) {
throw new DataAccessException("User save failed: " + user.id(), e);
}

If you rethrow, leave logging to the top boundary layer; do not both log and throw in the same place.