S4027 — Exceptions should provide standard constructors

Language
C#
Type
Code smell
Severity
Minor
Tags
convention

Why is this an issue?

Exceptions types should provide the following constructors:

The absence of these constructors can complicate exception handling and limit the information that can be provided when an exception is thrown.

How to fix it

Code examples

Noncompliant code example


public class MyException : Exception // Noncompliant: several constructors are missing
{
    public MyException()
    {
    }
}

Compliant solution


public class MyException : Exception
{
    public MyException()
    {
    }

    public MyException(string message)
        : base(message)
    {
    }

    public MyException(string message, Exception innerException)
        : base(message, innerException)
    {
    }
}

Resources

Documentation

↑ Back to top