次の方法で共有


引数の例外を正しくインスタンス化します

更新 : 2007 年 11 月

TypeName

InstantiateArgumentExceptionsCorrectly

CheckId

CA2208

カテゴリ

Microsoft.Usage

互換性に影響する変更点

なし

原因

ArgumentException、またはそのクラスから派生した例外の種類の、既定 (パラメータなし) のコンストラクタに対して呼び出しが行われました。

または

ArgumentException、またはそのクラスから派生した例外の種類の、パラメータ付きのコンストラクタに不適切な文字列型の引数が渡されました。

規則の説明

既定のコンストラクタを呼び出す代わりに、より意味のある例外メッセージを表示できるコンストラクタのオーバーロードを呼び出します。例外メッセージは開発者を対象にし、エラー条件、例外の修正方法、および例外の回避方法が明確にわかるようにします。

ArgumentException および派生型の、文字列型の引数が 1 つまたは 2 つである文字列コンストラクタのシグネチャは、message パラメータと paramName パラメータに関して整合性が取れていません。このようなコンストラクタは、正しい文字列型の引数で呼び出すようにします。次のようなシグネチャがあります。

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)

違反の修正方法

この規則違反を修正するには、メッセージ、パラメータ名、またはその両方を使用するコンストラクタを呼び出し、呼び出される ArgumentException の型に適した引数を指定します。

警告を抑制する状況

パラメータ付きのコンストラクタが正しい文字列型の引数で呼び出される場合のみ、この規則による警告を抑制しても安全です。

使用例

ArgumentNullException 型のインスタンスを正しくインスタンス化していないコンストラクタの例を次に示します。

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;            
            }        
        }    
    };
}

コンストラクタ引数を変更することによって上記の違反を修正するコード例を次に示します。

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;            
            }        
        }    
    };
}