次の方法で共有


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

TypeName

InstantiateArgumentExceptionsCorrectly

CheckId

CA2208

[カテゴリ]

Microsoft.Usage

互換性に影響する変更点

なし

原因

考えられる原因には次の場合があります。

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

  • [System.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;            
            }        
        }    
    };
}