File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
docs/fundamentals/code-analysis/quality-rules Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ helpviewer_keywords:
1010- DoNotRaiseReservedExceptionTypes
1111author : gewarren
1212ms.author : gewarren
13+ dev_langs :
14+ - CSharp
1315---
1416# CA2201: Do not raise reserved exception types
1517
@@ -78,6 +80,29 @@ For all other situations, consider creating your own type that derives from <xre
7880
7981To fix a violation of this rule, change the type of the thrown exception to a specific type that's not one of the reserved types.
8082
83+ ## Example
84+
85+ ``` csharp
86+ // This code violates the rule.
87+ throw new Exception ();
88+ throw new NullReferenceException ();
89+ throw new IndexOutOfRangeException ();
90+ // ...
91+
92+ // This code satisfies the rule.
93+ throw new ArgumentException ();
94+ throw new ArgumentNullException ();
95+ throw new InvalidOperationException ();
96+ // ...
97+
98+ // A minimal implementation of inheritance from Exception
99+ public class CustomException : Exception { }
100+
101+ // Or create your own type that derives from Exception
102+ // This code satisfies the rule too.
103+ throw new CustomException ();
104+ ```
105+
81106## When to suppress warnings
82107
83108Do not suppress a warning from this rule.
You can’t perform that action at this time.
0 commit comments