A catch clause only rethrows what it caught
What it finds
catch (Exception) { throw; } as the only handler catches and rethrows, which changes nothing and makes the code harder to read. Clauses followed by broader ones, where the rethrow keeps an exception away from the later handler, are left alone, and so are clauses with a filter.
From the rule's source, useless_catch.rs.
using System; class Pass{ void Run() { try { Work(); } catch (Exception)tx0 reports: this catch only rethrows the exception { throw; } try { Work(); } catch (ArgumentException) { throw; } catch (Exception e) { Console.WriteLine(e); } } void Work() { }} 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:useless-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:useless-catch] reason="..."tx0 explain tx0/csharp:useless-catch shows the same in the terminal. Found a false positive? Report it.