You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/code-quality/ca1032-implement-standard-exception-constructors.md
+28-14Lines changed: 28 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ ms.workload:
18
18
- "multiple"
19
19
---
20
20
# CA1032: Implement standard exception constructors
21
+
21
22
|||
22
23
|-|-|
23
24
|TypeName|ImplementStandardExceptionConstructors|
@@ -26,28 +27,41 @@ ms.workload:
26
27
|Breaking Change|Non-breaking|
27
28
28
29
## Cause
29
-
A type extends <xref:System.Exception?displayProperty=fullName> and does not declare all the required constructors.
30
30
31
-
## Rule Description
32
-
Exception types must implement the following constructors:
31
+
A type extends <xref:System.Exception?displayProperty=fullName> but doesn't declare all the required constructors.
32
+
33
+
## Rule description
34
+
35
+
Exception types must implement the following three constructors:
36
+
37
+
- public NewException()
38
+
39
+
- public NewException(string)
33
40
34
-
-public NewException()
41
+
- public NewException(string, Exception)
35
42
36
-
- public NewException(string)
43
+
Additionally, if you're running legacy FxCop static code analysis as opposed to [Roslyn-based FxCop analyzers](../code-quality/roslyn-analyzers-overview.md), the absence of a fourth constructor also generates a violation:
37
44
38
-
- public NewException(string, Exception)
45
+
-protected or private NewException(SerializationInfo, StreamingContext)
39
46
40
-
- protected or private NewException(SerializationInfo, StreamingContext)
47
+
Failure to provide the full set of constructors can make it difficult to correctly handle exceptions. For example, the constructor that has the signature `NewException(string, Exception)` is used to create exceptions that are caused by other exceptions. Without this constructor, you can't create and throw an instance of your custom exception that contains an inner (nested) exception, which is what managed code should do in such a situation.
41
48
42
-
Failure to provide the full set of constructors can make it difficult to correctly handle exceptions. For example, the constructor that has the signature `NewException(string, Exception)` is used to create exceptions that are caused by other exceptions. Without this constructor you cannot create and throw an instance of your custom exception that contains an inner (nested) exception, which is what managed code should do in such a situation. The first three exception constructors are public by convention. The fourth constructor is protected in unsealed classes, and private in sealed classes. For more information, see [CA2229: Implement serialization constructors](../code-quality/ca2229-implement-serialization-constructors.md)
49
+
The first three exception constructors are public by convention. The fourth constructor is protected in unsealed classes, and private in sealed classes. For more information, see [CA2229: Implement serialization constructors](../code-quality/ca2229-implement-serialization-constructors.md)
43
50
44
-
## How to Fix Violations
45
-
To fix a violation of this rule, add the missing constructors to the exception, and make sure that they have the correct accessibility.
51
+
## How to fix violations
46
52
47
-
## When to Suppress Warnings
48
-
It is safe to suppress a warning from this rule when the violation is caused by using a different access level for the public constructors.
53
+
To fix a violation of this rule, add the missing constructors to the exception, and make sure that they have the correct accessibility.
54
+
55
+
## When to suppress warnings
56
+
57
+
It's safe to suppress a warning from this rule when the violation is caused by using a different access level for the public constructors. Additionally, it's okay to suppress the warning for the `NewException(SerializationInfo, StreamingContext)` constructor if you're building a Portable Class Library (PCL).
49
58
50
59
## Example
51
-
The following example contains an exception type that violates this rule and an exception type that is correctly implemented.
0 commit comments