다음을 통해 공유


CA2208: 인수 예외를 올바르게 인스턴스화하십시오.

TypeName

InstantiateArgumentExceptionsCorrectly

CheckId

CA2208

범주

Microsoft.Usage

변경 수준

주요 변경 아님

원인

이 오류가 발생하는 원인은 다음과 같습니다.

  • [System.ArgumentException]에서 파생되었거나 예외 형식의 매개 변수가 없는 기본 생성자를 호출한 경우

  • [System.ArgumentException.]에서 파생되었거나 매개 변수가 있는 예외 형식의 생성자에 잘못된 문자열 인수를 전달한 경우

규칙 설명

기본 생성자를 호출하는 대신 보다 의미 있는 예외 메시지를 제공하는 생성자 오버로드 중 하나를 호출합니다. 예외 메시지는 개발자를 대상으로 하고 오류 조건과 예외 상황을 해결하거나 방지할 수 있는 방법을 명확하게 설명해야 합니다.

ArgumentException과 해당 파생 형식의 문자열 생성자 하나와 두 개에 대한 시그니처는 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;            
            }        
        }    
    };
}