ArgumentException ArgumentException ArgumentException Constructors

Definition

Overloads

ArgumentException() ArgumentException() ArgumentException()

Initializes a new instance of the ArgumentException class.

ArgumentException(String) ArgumentException(String) ArgumentException(String)

Initializes a new instance of the ArgumentException class with a specified error message.

ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext)

Initializes a new instance of the ArgumentException class with serialized data.

ArgumentException(String, Exception) ArgumentException(String, Exception) ArgumentException(String, Exception)

Initializes a new instance of the ArgumentException class with a specified error message and a reference to the inner exception that is the cause of this exception.

ArgumentException(String, String) ArgumentException(String, String) ArgumentException(String, String)

Initializes a new instance of the ArgumentException class with a specified error message and the name of the parameter that causes this exception.

ArgumentException(String, String, Exception) ArgumentException(String, String, Exception) ArgumentException(String, String, Exception)

Initializes a new instance of the ArgumentException class with a specified error message, the parameter name, and a reference to the inner exception that is the cause of this exception.

ArgumentException() ArgumentException() ArgumentException()

Initializes a new instance of the ArgumentException class.

public:
 ArgumentException();
public ArgumentException ();
Public Sub New ()

Remarks

This constructor initializes the Message property of the new instance to a system-supplied message that describes the error, such as "An invalid argument was specified." This message takes into account the current system culture.

The following table shows the initial property values for an instance of ArgumentException.

Property Value
InnerException A null reference (Nothing in Visual Basic).
Message The localized error message string.

ArgumentException(String) ArgumentException(String) ArgumentException(String)

Initializes a new instance of the ArgumentException class with a specified error message.

public:
 ArgumentException(System::String ^ message);
public ArgumentException (string message);
Public Sub New (message As String)
Parameters
message
String String String

The error message that explains the reason for the exception.

Remarks

This constructor initializes the Message property of the new instance to a system-supplied message that describes the error, such as "An invalid argument was specified." This message takes into account the current system culture.

The following table shows the initial property values for an instance of ArgumentException.

Property Value
InnerException A null reference (Nothing in Visual Basic).
Message The error message string.

ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext)

Initializes a new instance of the ArgumentException class with serialized data.

protected:
 ArgumentException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected ArgumentException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
Protected Sub New (info As SerializationInfo, context As StreamingContext)
Parameters
info
SerializationInfo SerializationInfo SerializationInfo

The object that holds the serialized object data.

context
StreamingContext StreamingContext StreamingContext

The contextual information about the source or destination.

Remarks

This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see XML and SOAP Serialization.

ArgumentException(String, Exception) ArgumentException(String, Exception) ArgumentException(String, Exception)

Initializes a new instance of the ArgumentException class with a specified error message and a reference to the inner exception that is the cause of this exception.

public:
 ArgumentException(System::String ^ message, Exception ^ innerException);
public ArgumentException (string message, Exception innerException);
Public Sub New (message As String, innerException As Exception)
Parameters
message
String String String

The error message that explains the reason for the exception.

innerException
Exception Exception Exception

The exception that is the cause of the current exception. If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.

Remarks

This constructor initializes the Message property of the new instance using the value of the message parameter. The content of the message parameter is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or null if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of ArgumentException.

Property Value
InnerException The inner exception reference.
Message The localized error message string.
See Also

ArgumentException(String, String) ArgumentException(String, String) ArgumentException(String, String)

Initializes a new instance of the ArgumentException class with a specified error message and the name of the parameter that causes this exception.

public:
 ArgumentException(System::String ^ message, System::String ^ paramName);
public ArgumentException (string message, string paramName);
Public Sub New (message As String, paramName As String)
Parameters
message
String String String

The error message that explains the reason for the exception.

paramName
String String String

The name of the parameter that caused the current exception.

Examples

The following code example demonstrates how to call the ArgumentException constructor. This code example is part of a larger example provided for the ArgumentException class.

int DivideByTwo(int num)
{
    // If num is an odd number, throw an ArgumentException.
    if ((num & 1) == 1)
    {
        throw gcnew ArgumentException("Number must be even", "num");
    }
    // num is even, return half of its value.
    return num / 2;
}
static int DivideByTwo(int num) 
{
    // If num is an odd number, throw an ArgumentException.
    if ((num & 1) == 1)
        throw new ArgumentException("Number must be even", "num");

    // num is even, return half of its value.
    return num / 2;
}

Remarks

This constructor initializes the Message property of the new instance using the value of the message parameter. The content of the message parameter is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

This constructor initializes the ParamName property of the new instance using paramName. The content of paramName is intended to be understood by humans.

The following table shows the initial property values for an instance of ArgumentException.

Property Value
Message The error message string.
ParamName The parameter name string.

ArgumentException(String, String, Exception) ArgumentException(String, String, Exception) ArgumentException(String, String, Exception)

Initializes a new instance of the ArgumentException class with a specified error message, the parameter name, and a reference to the inner exception that is the cause of this exception.

public:
 ArgumentException(System::String ^ message, System::String ^ paramName, Exception ^ innerException);
public ArgumentException (string message, string paramName, Exception innerException);
Public Sub New (message As String, paramName As String, innerException As Exception)
Parameters
message
String String String

The error message that explains the reason for the exception.

paramName
String String String

The name of the parameter that caused the current exception.

innerException
Exception Exception Exception

The exception that is the cause of the current exception. If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.

Remarks

This constructor initializes the Message property of the new instance using the value of the message parameter. The content of the message parameter is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

This constructor initializes the ParamName property of the new instance using paramName. The content of paramName is intended to be understood by humans.

An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or null if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of ArgumentException.

Property Value
InnerException The inner exception reference.
Message The localized error message string.
ParamName The parameter name string.
See Also

Applies to