ArgumentException Constructors

Definition

Initializes a new instance of the ArgumentException class.

Overloads

ArgumentException()

Initializes a new instance of the ArgumentException class.

ArgumentException(String)

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

ArgumentException(SerializationInfo, StreamingContext)
Obsolete.

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

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)

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)

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()

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.

Applies to

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 ArgumentException (string? message);
new ArgumentException : string -> ArgumentException
Public Sub New (message As String)

Parameters

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

Applies to

ArgumentException(SerializationInfo, StreamingContext)

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

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);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ArgumentException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new ArgumentException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArgumentException
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new ArgumentException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArgumentException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

Parameters

info
SerializationInfo

The object that holds the serialized object data.

context
StreamingContext

The contextual information about the source or destination.

Attributes

Remarks

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

See also

Applies to

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 ArgumentException (string? message, Exception? innerException);
new ArgumentException : string * Exception -> ArgumentException
Public Sub New (message As String, innerException As Exception)

Parameters

message
String

The error message that explains the reason for the exception.

innerException
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

Applies to

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 ArgumentException (string? message, string? paramName);
new ArgumentException : string * string -> ArgumentException
Public Sub New (message As String, paramName As String)

Parameters

message
String

The error message that explains the reason for the exception.

paramName
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;
}
let divideByTwo num =
    // If num is an odd number, raise an ArgumentException.
    if num % 2 = 1 then
        raise (ArgumentException("num", "Number must be even"))

    // num is even, return half of its value.
    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.

Applies to

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 ArgumentException (string? message, string? paramName, Exception? innerException);
new ArgumentException : string * string * Exception -> ArgumentException
Public Sub New (message As String, paramName As String, innerException As Exception)

Parameters

message
String

The error message that explains the reason for the exception.

paramName
String

The name of the parameter that caused the current exception.

innerException
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