2021-04-28 16:49:39 +02:00
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.
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== 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
}
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
try
{}
catch(ExceptionType1 exc)
{
Console.WriteLine(exc);
throw;
}
catch (ExceptionType2 exc)
{
throw new Exception("My custom message", exc);
}
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]