A try with handlers is nested in the block of another one

tx0/java:nested-trystrictwarningstyle

What it finds

A try with handlers inside the block of another try with handlers splits the error handling of one piece of code over two levels, and a reader has to work out which handler sees which exception. A method for the inner part, or one try with more handlers, is clearer.

Only try statements with catch clauses count, inside and outside: a try that only closes resources or runs a finally block is not error handling of its own.

From the rule's source, nested_try.rs.

Example from the rule's tests
class Files {  void copy() {    try {      try {tx0 reports: this try is nested in the block of another try        read();      } catch (RuntimeException e) {        log(e);      }      try {        read();      } finally {        close();      }    } catch (Exception e) {      try {        log(e);      } catch (RuntimeException again) {        close();      }    }  }   void read() {}  void close() {}  void log(Exception e) {}}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:nested-try" = "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:nested-try] reason="..."

tx0 explain tx0/java:nested-try shows the same in the terminal. Found a false positive? Report it.