An exception is created and dropped instead of thrown

tx0/java:unthrown-exceptiondefaultwarningbug

What it finds

new IllegalStateException("closed"); on a line of its own: the exception is created and dropped, and nothing is thrown. throw was left out.

The class must be one of the common exceptions of java.lang, java.io and java.util, named so that it resolves to that class, or a class of this file that extends one, directly or through other such classes. A statement directly in a try block is left alone, since tests create exceptions with bad arguments there to check that this fails.

From the rule's source, unthrown_exception.rs.

Example from the rule's tests
import java.io.IOException; class Checks {  static class AppException extends RuntimeException {}   static class NotFound extends AppException {}   static class Plain {}   static class IllegalStateException {}   Object check(int n) throws IOException {    if (n < 0) {      new IllegalArgumentException("negative");tx0 reports: this IllegalArgumentException is created but not thrown    }    if (n == 0) {      new NotFound();tx0 reports: this NotFound is created but not thrown    }    if (n == 1) {      new java.lang.UnsupportedOperationException();tx0 reports: this java.lang.UnsupportedOperationException is created but not thrown    }    if (n == 2) {      new IOException("io");tx0 reports: this IOException is created but not thrown    }    if (n == 3) {      throw new IllegalArgumentException("thrown");    }    new Plain();    new IllegalStateException();    new RuntimeException() {      @Override      public String getMessage() {        return "anonymous";      }    };    try {      new IllegalArgumentException((String) null, null);    } catch (NullPointerException e) {      n++;    }    switch (n) {      case 4 -> new IllegalArgumentException();tx0 reports: this IllegalArgumentException is created but not thrown      default -> n++;    }    return switch (n) {      case 5 -> new IllegalArgumentException();      default -> new RuntimeException();    };  }}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:unthrown-exception" = "off"

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:unthrown-exception] reason="..."

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