🔧 One Word Difference Destroys Your Stack Trace
You catch an exception, log some context, and re-throw it with throw ex; so the caller still sees the error. Except now the stack trace points at your catch block instead of the line that actually failed — the real bug location is gone forever.
🐞 The Problem
try
{
ProcessOrder(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Order processing failed");
throw ex; // <- resets the stack trace right here
}
🔍 Why This Happens
"throw ex;" is treated by the runtime as a brand new throw statement. The exception object is the same, but its StackTrace property gets overwritten to start at THIS line, not the line where the exception was originally raised inside ProcessOrder(). By the time it reaches your top-level error handler, every clue about where things actually went wrong is gone.
✅ The Fix: Bare throw; Preserves Everything
- Replace
throw ex;with a barethrow;— same exception, same message, but the original stack trace stays intact all the way down to the failing line. - If you need to add context, wrap it instead:
throw new InvalidOperationException("Order processing failed", ex);— the inner exception still carries its full original trace.
📦 Correct Version
try
{
ProcessOrder(order);
}
catch (Exception ex)
{
_logger.LogError(ex, "Order processing failed");
throw; // preserves the original stack trace
}
⚠️ Quick Rule of Thumb
- Never type
throw ex;in a catch block — if a code review sees it, it should be flagged every time. - Static analyzers (Roslyn CA2200) already catch this one automatically — enable it as a build warning-as-error if it isn’t already.
The exception object survives either way — only the trail of breadcrumbs leading back to the real bug depends on which keyword you type.

