Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

CA2208: Instantiate argument exceptions correctly

TypeName

InstantiateArgumentExceptionsCorrectly

CheckId

CA2208

Category

Microsoft.Usage

Breaking Change

Non Breaking

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.]

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)

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.

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

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


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


Community Additions

ADD
Show:
© 2015 Microsoft