Поделиться через


CA2208: Instantiate argument exceptions correctly

TypeName

InstantiateArgumentExceptionsCorrectly

CheckId

CA2208

Category

Microsoft.Usage

Breaking Change

Non Breaking

Cause

Possible causes include the following situations:

  • A call is made to the default (parameterless) constructor of an exception type that is, or derives from [System.ArgumentException].

  • An incorrect string argument is passed to a parameterized constructor of an exception type that is, or derives from [System.ArgumentException.]

Rule Description

Instead of calling the default constructor, call one of the constructor overloads that allows a more meaningful exception message to be provided. The exception message should target the developer and clearly explain the error condition and how to correct or avoid the exception.

The signatures of the one and two string constructors of ArgumentException and its derived types are not consistent with respect to the message and paramName parameters. Make sure these constructors are called with the correct string arguments. The signatures are as follows:

ArgumentException(string message)

ArgumentException(string message, string paramName)

ArgumentNullException(string paramName)

ArgumentNullException(string paramName, string message)

ArgumentOutOfRangeException(string paramName)

ArgumentOutOfRangeException(string paramName, string message)

DuplicateWaitObjectException(string parameterName)

DuplicateWaitObjectException(string parameterName, string message)

How to Fix Violations

To fix a violation of this rule, call a constructor that takes a message, a parameter name, or both, and make sure the arguments are proper for the type of ArgumentException being called.

When to Suppress Warnings

It is safe to suppress a warning from this rule only if a parameterized constructor is called with the correct string arguments.

Example

The following example shows a constructor that incorrectly instantiates an instance of the ArgumentNullException type.

Imports System

Namespace Samples1

    Public Class Book

        Private ReadOnly _Title As String

        Public Sub New(ByVal title As String)
            ' Violates this rule (constructor arguments are switched)            
            If (title Is Nothing) Then
                Throw New ArgumentNullException("title cannot be a null reference (Nothing in Visual Basic)", "title")
            End If
            _Title = title
        End Sub

        Public ReadOnly Property Title()
            Get
                Return _Title
            End Get
        End Property

    End Class

End Namespace
using System;

namespace Samples1
{    
    public class Book    
    {        
        private readonly string _Title;

        public Book(string title)        
        {            
            // Violates this rule (constructor arguments are switched)            
            if (title == null)                
                throw new ArgumentNullException("title cannot be a null reference (Nothing in Visual Basic)", "title");

            _Title = title;        
        }

        public string Title        
        {            
            get { return _Title; }        
        }
    }
}
using namespace System;

namespace Samples1 
{    
    public ref class Book    
    {
     private: initonly String^ _Title;

    public:
        Book(String^ title)        
        {            
            // Violates this rule (constructor arguments are switched)            
            if (title == nullptr)                
                throw gcnew ArgumentNullException("title cannot be a null reference (Nothing in Visual Basic)", "title");

            _Title = title;        
        }

        property String^ Title        
        {            
            String^ get()            
            {                
                return _Title;            
            }        
        }    
    };
}

The following example fixes the above violation by switching the constructor arguments.

Namespace Samples2

    Public Class Book

        Private ReadOnly _Title As String

        Public Sub New(ByVal title As String)
            If (title Is Nothing) Then
                Throw New ArgumentNullException("title", "title cannot be a null reference (Nothing in Visual Basic)")
            End If

            _Title = title
        End Sub

        Public ReadOnly Property Title()
            Get
                Return _Title
            End Get
        End Property

    End Class

End Namespace
namespace Samples2
{    
    public class Book    
    {        
        private readonly string _Title;

        public Book(string title)        
        {            
            if (title == null)                
                throw new ArgumentNullException("title", "title cannot be a null reference (Nothing in Visual Basic)");

            _Title = title;        }

        public string Title        
        {            
            get { return _Title; }        
        }
    }
}
using namespace System;

namespace Samples2 
{    
    public ref class Book    
    {
     private: initonly String^ _Title;

    public:
        Book(String^ title)        
        {            
            if (title == nullptr)                
                throw gcnew ArgumentNullException(("title", "title cannot be a null reference (Nothing in Visual Basic)"));

            _Title = title;        
        }

        property String^ Title        
        {            
            String^ get()            
            {                
                return _Title;            
            }        
        }    
    };
}