C# Interview Question #52

What is the difference between throw and throw ex in C#?

Exceptions, Delegates, Events & Lambdas Mid-Level Intermediate

Quick Interview Answer

When rethrowing the current exception from a catch block, throw; preserves the original stack trace. Using throw ex; resets important stack-trace information to the current location, making debugging harder.

Detailed Explanation

When rethrowing the current exception from a catch block, throw; preserves the original stack trace. Using throw ex; resets important stack-trace information to the current location, making debugging harder.

Therefore, throw; is normally the correct choice when the same exception needs to propagate.

If a new exception is required to add domain-specific context, the original exception should usually be supplied as the inner exception.

Code Example

try
{
    SaveData();
}
catch (Exception)
{
    throw; // Preserves original stack trace
}