rspec/rules/S3445/csharp/rule.adoc
2021-04-28 18:08:03 +02:00

37 lines
658 B
Plaintext

When rethrowing an exception, you should do it by simply calling ``++throw;++`` and not ``++throw exc;++``, because the stack trace is reset with the second syntax, making debugging a lot harder.
== Noncompliant Code Example
----
try
{}
catch(ExceptionType1 exc)
{
Console.WriteLine(exc);
throw exc; // Noncompliant; stacktrace is reset
}
catch (ExceptionType2 exc)
{
throw new Exception("My custom message", exc); // Compliant; stack trace preserved
}
----
== Compliant Solution
----
try
{}
catch(ExceptionType1 exc)
{
Console.WriteLine(exc);
throw;
}
catch (ExceptionType2 exc)
{
throw new Exception("My custom message", exc);
}
----