Skip to content

Re-throwing Exceptions Correctly

To preserve the exception stacktrace you should only issue the throw command, not rethrow the variable, so don't do this:

try
{
    // Do something
}
catch (exception ex)
{
    _logger.LogError(...)

    // Do not do the following
    throw ex;
}

Instead, do the following:

try
{
    // Do something
}
catch (exception ex)
{
    _logger.LogError(...)

    // This is correct
    throw;
}

This re-throws the exact same exception with the full stack trace.