ArgumentException 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当向方法提供的参数之一无效时引发的异常。
public ref class ArgumentException : Exceptionpublic ref class ArgumentException : SystemExceptionpublic class ArgumentException : Exceptionpublic class ArgumentException : SystemException[System.Serializable]
public class ArgumentException : SystemException[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArgumentException : SystemExceptiontype ArgumentException = class
    inherit Exceptiontype ArgumentException = class
    inherit SystemExceptiontype ArgumentException = class
    inherit SystemException
    interface ISerializable[<System.Serializable>]
type ArgumentException = class
    inherit SystemException
    interface ISerializable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArgumentException = class
    inherit SystemException
    interface ISerializablePublic Class ArgumentException
Inherits ExceptionPublic Class ArgumentException
Inherits SystemException- 继承
- 继承
- 派生
- 属性
- 实现
示例
以下示例演示如何引发和捕获 ArgumentException。 它使用 ArgumentException.GetType () 。Name 属性用于显示异常对象的名称,还使用 Message 属性显示异常消息的文本。
using namespace System;
static int DivideByTwo(int num) 
{
    // If num is an odd number, throw an ArgumentException.
    if ((num & 1) == 1)
        throw gcnew ArgumentException(String::Format("{0} is not an even number", num), 
                                      "num");
    // num is even, return half of its value.
    return num / 2;
}
void main() 
{
    // Define some integers for a division operation.
    array<int>^ values = { 10, 7 };
    for each (int value in values) {
        try {
           Console::WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value));
        }
        catch (ArgumentException^ e) {
           Console::WriteLine("{0}: {1}", e->GetType()->Name, e->Message);
        }
        Console::WriteLine();
    }
}
// This example displays the following output:
//     10 divided by 2 is 5
//     
//     ArgumentException: 7 is not an even number
//     Parameter name: num
using System;
public class Example
{
    static void Main()
    {
        // Define some integers for a division operation.
        int[] values = { 10, 7 };
        foreach (var value in values) {
            try {
               Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value));
            }
            catch (ArgumentException e) {
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
            }
            Console.WriteLine();
        }
    }
    static int DivideByTwo(int num)
    {
        // If num is an odd number, throw an ArgumentException.
        if ((num & 1) == 1)
            throw new ArgumentException(String.Format("{0} is not an even number", num),
                                      "num");
        // num is even, return half of its value.
        return num / 2;
    }
}
// This example displays the following output:
//     10 divided by 2 is 5
//
//     ArgumentException: 7 is not an even number
//     Parameter name: num
open System
let divideByTwo num =
    // If num is an odd number, throw an ArgumentException.
    if num % 2 = 1 then
        invalidArg "num" $"{num} is not an even number"
    // num is even, return half of its value.
    num / 2;
// Define some integers for a division operation.
let values = [ 10; 7 ]
for value in values do
    try 
        printfn $"{value} divided by 2 is {divideByTwo value}"
    with
    | :? ArgumentException as e ->
        printfn $"{e.GetType().Name}: {e.Message}"
    
    printfn ""
// This example displays the following output:
//     10 divided by 2 is 5
//
//     ArgumentException: 7 is not an even number (Parameter 'num')
Public Class Example
    Public Shared Sub Main()
        ' Define some integers for a division operation.
        Dim values() As Integer = { 10, 7 }
        For Each value In values
            Try 
               Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value))
            Catch e As ArgumentException
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
            End Try
            Console.WriteLine()
        Next
    End Sub
    
    Private Shared Function DivideByTwo(ByVal num As Integer) As Integer
        ' If num is an odd number, throw an ArgumentException.
        If (num And 1) = 1 Then
            Throw New ArgumentException(String.Format("{0} is not an even number", num), 
                                      "num")
        End If
        Return num \ 2
    End Function
End Class
' The example displays the following output:
'     10 divided by 2 is 5
'     
'     ArgumentException: 7 is not an even number
'     Parameter name: num
注解
ArgumentException 当调用方法并且至少有一个传递的参数不符合所调用方法的参数规范时,将引发 。 属性 ParamName 标识无效参数。
最常见的情况是, ArgumentException 由公共语言运行时或其他类库引发,指示开发人员错误。 如果从代码中引发 ArgumentException ,则应确保异常的 Message 属性包含一条有意义的错误消息,用于描述无效参数和参数的预期值范围。
的主要派生类 ArgumentException 是 ArgumentNullException 和 ArgumentOutOfRangeException。 应使用这些派生类而不是 ArgumentException,除非在两个派生类都不能接受的情况下。 例如,应通过以下方式引发异常:
- ArgumentNullException 每当 - null传递给不接受它作为有效参数的方法时。
- ArgumentOutOfRangeException 当参数的值超出可接受的值范围时;例如,在创建 DateTime期间将值“46”作为 month 参数传递时。 
如果方法调用没有任何参数,或者如果失败不涉及参数本身,则应 InvalidOperationException 使用 。
ArgumentException 使用 HRESULT COR_E_ARGUMENT,其值0x80070057。
有关实例的初始属性值的列表ArgumentException,请参阅ArgumentException构造函数。
在 F# 中,可以使用 invalidArg 函数生成并引发 ArgumentException。
构造函数
| ArgumentException() | 初始化 ArgumentException 类的新实例。 | 
| ArgumentException(SerializationInfo, StreamingContext) | 
				已过时.
			 用序列化数据初始化 ArgumentException 类的新实例。 | 
| ArgumentException(String) | 用指定的错误消息初始化 ArgumentException 类的新实例。 | 
| ArgumentException(String, Exception) | 使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 ArgumentException 类的新实例。 | 
| ArgumentException(String, String) | 使用指定的错误消息和导致此异常的参数的名称来初始化 ArgumentException 类的新实例。 | 
| ArgumentException(String, String, Exception) | 使用指定的错误消息、参数名称和对内部异常的引用(为该异常根源)来初始化 ArgumentException 类的新实例。 | 
属性
| Data | 获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。(继承自 Exception) | 
| HelpLink | 获取或设置指向与此异常关联的帮助文件链接。(继承自 Exception) | 
| HResult | 获取或设置 HRESULT(一个分配给特定异常的编码数字值)。(继承自 Exception) | 
| InnerException | 获取导致当前异常的 Exception 实例。(继承自 Exception) | 
| Message | 获取错误消息和参数名;如果未设置参数名,则仅获取错误消息。 | 
| ParamName | 获取导致该异常的参数的名称。 | 
| Source | 获取或设置导致错误的应用程序或对象的名称。(继承自 Exception) | 
| StackTrace | 获取调用堆栈上的即时框架字符串表示形式。(继承自 Exception) | 
| TargetSite | 获取引发当前异常的方法。(继承自 Exception) | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetBaseException() | 当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。(继承自 Exception) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetObjectData(SerializationInfo, StreamingContext) | 
				已过时.
			 设置带有参数名和附加异常信息的 SerializationInfo 对象。 | 
| GetObjectData(SerializationInfo, StreamingContext) | 
		已过时.
	 当在派生类中重写时,用关于异常的信息设置 SerializationInfo。(继承自 Exception) | 
| GetType() | 获取当前实例的运行时类型。(继承自 Exception) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ThrowIfNullOrEmpty(String, String) | 如果  | 
| ThrowIfNullOrWhiteSpace(String, String) | 如果  | 
| ToString() | 创建并返回当前异常的字符串表示形式。(继承自 Exception) | 
事件
| SerializeObjectState | 
		已过时.
	 当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。(继承自 Exception) |