ArgumentException 构造函数

定义

初始化 ArgumentException 类的新实例。Initializes a new instance of the ArgumentException class.

重载

ArgumentException()

初始化 ArgumentException 类的新实例。Initializes a new instance of the ArgumentException class.

ArgumentException(String)

用指定的错误消息初始化 ArgumentException 类的新实例。Initializes a new instance of the ArgumentException class with a specified error message.

ArgumentException(SerializationInfo, StreamingContext)

用序列化数据初始化 ArgumentException 类的新实例。Initializes a new instance of the ArgumentException class with serialized data.

ArgumentException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArgumentException 类的新实例。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 类的新实例。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 类的新实例。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 类的新实例。Initializes a new instance of the ArgumentException class.

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

注解

此构造函数将 Message 新实例的属性初始化为系统提供的描述错误的消息,如 "指定了无效的参数。"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.

下表显示了 ArgumentException 实例的初始属性值。The following table shows the initial property values for an instance of ArgumentException.

propertiesProperty Value
InnerException 空引用(在 Visual Basic 中为 Nothing)。A null reference (Nothing in Visual Basic).
Message 本地化的错误消息字符串。The localized error message string.

ArgumentException(String)

用指定的错误消息初始化 ArgumentException 类的新实例。Initializes a new instance of the ArgumentException class with a specified error message.

public:
 ArgumentException(System::String ^ message);
public ArgumentException (string message);
new ArgumentException : string -> ArgumentException
Public Sub New (message As String)

参数

message
String

解释异常原因的错误消息。The error message that explains the reason for the exception.

注解

此构造函数将 Message 新实例的属性初始化为系统提供的描述错误的消息,如 "指定了无效的参数。"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.

下表显示了 ArgumentException 实例的初始属性值。The following table shows the initial property values for an instance of ArgumentException.

propertiesProperty Value
InnerException 空引用(在 Visual Basic 中为 Nothing)。A null reference (Nothing in Visual Basic).
Message 错误消息字符串。The error message string.

ArgumentException(SerializationInfo, StreamingContext)

用序列化数据初始化 ArgumentException 类的新实例。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);
new ArgumentException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArgumentException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

承载序列化对象数据的对象。The object that holds the serialized object data.

context
StreamingContext

关于来源和目标的上下文信息The contextual information about the source or destination.

注解

在反序列化期间调用此构造函数以重新构造在流上传输的异常对象。This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. 有关详细信息,请参阅 XML 和 SOAP 序列化For more information, see XML and SOAP Serialization.

另请参阅

ArgumentException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArgumentException 类的新实例。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);
new ArgumentException : string * Exception -> ArgumentException
Public Sub New (message As String, innerException As Exception)

参数

message
String

解释异常原因的错误消息。The error message that explains the reason for the exception.

innerException
Exception

导致当前异常的异常。The exception that is the cause of the current exception. 如果 innerException 参数不为空引用,则在处理内部异常的 catch 块中引发当前异常。If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.

注解

此构造函数 Message 使用参数的值初始化新实例的属性 messageThis constructor initializes the Message property of the new instance using the value of the message parameter. message 参数的内容应为人所理解。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.

因前一个异常而直接引发的异常应在 InnerException 属性中包含对前一个异常的引用。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. InnerException 属性将返回传递给构造函数的同一值;如果 InnerException 属性不向构造函数提供内部异常值,则返回 nullThe 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.

下表显示了 ArgumentException 实例的初始属性值。The following table shows the initial property values for an instance of ArgumentException.

propertiesProperty “值”Value
InnerException 内部异常引用。The inner exception reference.
Message 本地化的错误消息字符串。The localized error message string.

另请参阅

ArgumentException(String, String)

使用指定的错误消息和导致此异常的参数的名称来初始化 ArgumentException 类的新实例。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);
new ArgumentException : string * string -> ArgumentException
Public Sub New (message As String, paramName As String)

参数

message
String

解释异常原因的错误消息。The error message that explains the reason for the exception.

paramName
String

导致当前异常的参数的名称。The name of the parameter that caused the current exception.

示例

下面的代码示例演示如何调用 ArgumentException 构造函数。The following code example demonstrates how to call the ArgumentException constructor. 此代码示例是为类提供的更大示例的一部分 ArgumentExceptionThis 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;
}

注解

此构造函数 Message 使用参数的值初始化新实例的属性 messageThis constructor initializes the Message property of the new instance using the value of the message parameter. message 参数的内容应为人所理解。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.

此构造函数 ParamName 使用初始化新实例的属性 paramNameThis constructor initializes the ParamName property of the new instance using paramName. paramName 的内容设计为易于理解。The content of paramName is intended to be understood by humans.

下表显示了 ArgumentException 实例的初始属性值。The following table shows the initial property values for an instance of ArgumentException.

propertiesProperty ValueValue
Message 错误消息字符串。The error message string.
ParamName 参数名称字符串。The parameter name string.

ArgumentException(String, String, Exception)

使用指定的错误消息、参数名称和对内部异常的引用(为该异常根源)来初始化 ArgumentException 类的新实例。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);
new ArgumentException : string * string * Exception -> ArgumentException
Public Sub New (message As String, paramName As String, innerException As Exception)

参数

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. 如果 innerException 参数不为空引用,则在处理内部异常的 catch 块中引发当前异常。If the innerException parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.

注解

此构造函数 Message 使用参数的值初始化新实例的属性 messageThis constructor initializes the Message property of the new instance using the value of the message parameter. message 参数的内容应为人所理解。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.

此构造函数 ParamName 使用初始化新实例的属性 paramNameThis constructor initializes the ParamName property of the new instance using paramName. paramName 的内容设计为易于理解。The content of paramName is intended to be understood by humans.

因前一个异常而直接引发的异常应在 InnerException 属性中包含对前一个异常的引用。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. InnerException 属性将返回传递给构造函数的同一值;如果 InnerException 属性不向构造函数提供内部异常值,则返回 nullThe 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.

下表显示了 ArgumentException 实例的初始属性值。The following table shows the initial property values for an instance of ArgumentException.

propertiesProperty “值”Value
InnerException 内部异常引用。The inner exception reference.
Message 本地化的错误消息字符串。The localized error message string.
ParamName 参数名称字符串。The parameter name string.

另请参阅

适用于