rspec/rules/S3445/csharp/rule.adoc

58 lines
1000 B
Plaintext

== Why is this an issue?
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
[source,csharp]
----
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
[source,csharp]
----
try
{}
catch(ExceptionType1 exc)
{
Console.WriteLine(exc);
throw;
}
catch (ExceptionType2 exc)
{
throw new Exception("My custom message", exc);
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]