A catch throws a new exception without passing the caught one as its inner exception
What it finds
Wrapping an exception in a more meaningful one is good practice, but the wrapper should carry the original as its InnerException: without it, the stack trace and message of the actual failure are lost, and the log shows only where it was translated. The rule reports throw new ... inside a catch that names the caught exception, when the new exception's arguments do not mention it.
From the rule's source, cause_dropped_in_catch.rs.
using System;using System.IO; class Loader{ string Read(string path) { try { return File.ReadAllText(path); } catch (IOException e) { throw new InvalidOperationException("cannot read " + path);tx0 reports: the new exception does not keep e as its cause } } string Wrapped(string path) { try { return File.ReadAllText(path); } catch (IOException e) { throw new InvalidOperationException("cannot read " + path, e); } }} 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:
[rules]
"tx0/csharp:cause-dropped-in-catch" = "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/csharp:cause-dropped-in-catch] reason="..."tx0 explain tx0/csharp:cause-dropped-in-catch shows the same in the terminal. Found a false positive? Report it.