A caught exception is thrown again with throw ex;, which loses its stack trace

tx0/csharp:rethrow-loses-stackdefaultwarningreliability/error-handlingdepth fastbug

What it finds

Inside catch (Exception ex), throw ex; throws the exception as if it started here: the stack trace that showed where it came from is replaced. throw; rethrows it with the trace intact; to add context, throw a new exception with ex as its inner exception.

From the rule's source, rethrow_loses_stack.rs.

Example from the rule's tests
using System; class Retry{    void Run()    {        try        {            Work();        }        catch (InvalidOperationException ex)        {            Log(ex);            throw ex;tx0 reports: throw ex; replaces the stack trace of the caught exception        }        catch (Exception ex)        {            throw new ApplicationException("failed", ex);        }    }     void Work() { }    void Log(Exception e) { }}
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/csharp:rethrow-loses-stack" = "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/csharp:rethrow-loses-stack] reason="..."

tx0 explain tx0/csharp:rethrow-loses-stack shows the same in the terminal. Found a false positive? Report it.