Skip to content

Commit 0c6575d

Browse files
authored
Add code example for CA2201 rule (#49058) (#49059)
1 parent 225d23b commit 0c6575d

File tree

1 file changed

+25
-0
lines changed
  • docs/fundamentals/code-analysis/quality-rules

1 file changed

+25
-0
lines changed

docs/fundamentals/code-analysis/quality-rules/ca2201.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ helpviewer_keywords:
1010
- DoNotRaiseReservedExceptionTypes
1111
author: gewarren
1212
ms.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

7981
To 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

83108
Do not suppress a warning from this rule.

0 commit comments

Comments
 (0)