Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 1.02 KB

File metadata and controls

57 lines (42 loc) · 1.02 KB
layout page
title CC0012
tagline RethrowExceptionAnalyzer

|TypeName|RethrowExceptionAnalyzer | |Check Id | CC0012 | |Category | Naming | |Severity | Warning |

Cause

Throwing the same exception as passed to the 'catch' block lose the original stack trace and will make debugging this exception a lot more difficult.
The correct way to rethrow an exception without changing it is by using 'throw' without any parameter.

Example

{% highlight csharp %} try { } catch (System.Exception ex) { throw ex; } {% endhighlight %}

Code fix

You will have two choices: you can rethrow the original exception as is:

{% highlight csharp %} try { } catch (System.Exception ex) { throw; } {% endhighlight %}

Or you can rethrow the original exception as an inner exception, like so:

{% highlight csharp %} try { } catch (System.Exception ex) { throw new Exception("some reason to rethrow", ex); } {% endhighlight %}

![Code fix]({{ BASE_PATH }}/assets/images/CC0012/codefix0.png)

Related rules

None

See also

TBD