An exception is thrown from a catch block without the caught exception as its cause

tx0/java:lost-exception-causestrictwarningsmell

What it finds

catch (IOException e) { throw new IllegalStateException("cannot read"); } throws a new exception without the one that was caught: its message and stack trace, which say what actually went wrong, are lost. Passing e as the cause keeps them.

Reported is a throw new ... in a catch block that passes neither the caught exception nor something taken from it other than its message to any constructor or method in the thrown expression (nor the exception of a catch around it), in a block that calls neither initCause nor addSuppressed.

From the rule's source, lost_exception_cause.rs.

Example from the rule's tests
import java.io.IOException; class Loader {  byte[] read() {    try {      return load();    } catch (IOException e) {      throw new IllegalStateException("cannot read");tx0 reports: the caught exception e is not passed on as the cause    }  }   byte[] readMessage() {    try {      return load();    } catch (IOException e) {      throw new IllegalStateException(e.getMessage());tx0 reports: the caught exception e is not passed on    }  }   byte[] readWrapped() {    try {      return load();    } catch (IOException e) {      throw new IllegalStateException("cannot read", e);    }  }   byte[] readCause() {    try {      return load();    } catch (IOException e) {      throw new IllegalStateException(e.getCause());    }  }   byte[] readInit() {    try {      return load();    } catch (IOException e) {      IllegalStateException failure = new IllegalStateException("cannot read");      failure.initCause(e);      throw failure;    }  }   byte[] readNested() {    try {      return load();    } catch (IOException e) {      Runnable r = () -> {        throw new IllegalStateException("later");      };      throw new IllegalStateException(wrap(e));    }  }   static String wrap(Exception e) {    return e.toString();  }   byte[] load() throws IOException {    return new byte[0];  }}
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:lost-exception-cause" = "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:lost-exception-cause] reason="..."

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