A caught exception is logged and then thrown again, so it is logged twice or more

tx0/java:log-and-rethrowrecommendedwarningsmell

What it finds

A handler that logs an exception and rethrows it leaves the decision to the caller, which usually logs it again: the same failure appears several times in the log, with the same stack trace. Either handling it here or passing it on, not both, keeps one entry per failure.

Reported is a catch block that passes the caught exception to a method of a receiver named like a logger and then throws the same exception.

From the rule's source, log_and_rethrow.rs.

Example from the rule's tests
import org.slf4j.Logger; class Worker {  private Logger logger;   void run() throws Exception {    try {      work();    } catch (Exception e) {      logger.error("failed", e);      throw e;tx0 reports: logged twice    }    try {      work();    } catch (Exception e) {      throw e;    }  }   void work() throws Exception {}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:log-and-rethrow" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:log-and-rethrow] reason="..."

tx0 explain tx0/java:log-and-rethrow shows the same in the terminal. Found a false positive? Report it.