A caught exception is tested with instanceof instead of being caught by its own type

tx0/java:instanceof-on-caughtstrictwarningsmell

What it finds

catch (Exception e) { if (e instanceof IOException) ... } does by hand what a catch (IOException e) clause does, and the broad handler also catches exceptions it then ignores. Separate catch clauses, or a multi-catch, say which exceptions are handled.

Reported is an instanceof test of the parameter of the catch clause it is in.

From the rule's source, instanceof_on_caught.rs.

Example from the rule's tests
import java.io.IOException; class Handler {  void run() {    try {      work();    } catch (Exception e) {      if (e instanceof IOException) {tx0 reports: a catch clause of that type does it        System.out.println("io");      }    }  }   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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

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

tx0 explain tx0/java:instanceof-on-caught shows the same in the terminal. Found a false positive? Report it.