All rules
CA1032Design Enabled by default: No

Implement standard exception constructors

Implement standard exception constructors

Microsoft docs

Description

Exception types must implement the following three public constructors:

  • public NewException()
  • public NewException(string)
  • public NewException(string, Exception)

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.

For more information, see CA2229: Implement serialization constructors.

Cause

A type extends System.Exception but doesn't declare all the required constructors.

How to fix violations

To fix a violation of this rule, add the missing constructors to the exception, and make sure that they have the correct accessibility.

Example

#pragma warning disable CA1032
// The code that's violating the rule is on this line.
#pragma warning restore CA1032

When to suppress

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.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0