ArgumentException 类
2013/3/11
在向方法提供的其中一个参数无效时引发的异常。
Namespace:
System
程序集: mscorlib(位于 mscorlib.dll 中)
ArgumentException 类型公开以下成员。
| 名称 | 说明 | |
|---|---|---|
![]() | ArgumentException() | 初始化 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 | 获取错误消息和参数名;如果未设置参数名,则仅获取错误消息。 (重写 Exception.Message。) |
![]() | ParamName | 获取导致该异常的参数的名称。 |
![]() | Source | 获取或设置导致错误的应用程序或对象的名称。 (从 Exception 继承。) |
![]() | StackTrace | 获取在引发当前异常时调用堆栈上帧的字符串表示形式。 (从 Exception 继承。) |
| 名称 | 说明 | |
|---|---|---|
![]() | Equals(Object) | 确定指定的 Object 是否等于当前的 Object。 (从 Object 继承。) |
![]() | Finalize | 允许 Object 在垃圾回收器回收该对象之前尝试释放资源并执行其他清理操作。 (从 Object 继承。) |
![]() | GetBaseException | 当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。 (从 Exception 继承。) |
![]() | GetHashCode | 用作特定类型的哈希函数。 (从 Object 继承。) |
![]() | GetType | 获取当前实例的运行时类型。 (从 Exception 继承。) |
![]() | MemberwiseClone | 创建当前 Object 的浅表副本。 (从 Object 继承。) |
![]() | ToString | 创建并返回当前异常的字符串表示形式。 (从 Exception 继承。) |
在调用某方法但传递的参数中至少有一个不符合所调用方法的参数规范时,将引发 ArgumentException。ArgumentException 的所有实例均应带有有意义的错误消息,描述无效参数以及该参数所需的值范围。
ArgumentException 的主要派生类有 ArgumentNullException 和 ArgumentOutOfRangeException。应使用这两种派生类取代 ArgumentException,除非这两种派生类都不被接受。例如,异常应由以下内容引发:
每当向方法传递 null 而该方法不把它作为有效参数接受时,应由 ArgumentNullException 引发异常。
当参数值超出可接受值的范围(例如,在创建 DateTime 时将值“46”作为月份参数传递)时,应由 ArgumentOutOfRangeException 引发异常。
如果方法调用没有任何参数,或者失败未涉及参数本身,则应当使用 InvalidOperationException 引发异常。
ArgumentException 使用值为 0x80070057 的 HRESULT COR_E_ARGUMENT。
有关 ArgumentException 实例的初始属性值列表,请参见 ArgumentException 构造函数。
版本说明
Windows Phone
ArgumentException 显示消息“参数异常”而不是“类型为‘System.ArgumentException’的异常”。下面的示例演示如何引发和捕捉 ArgumentException。
注意: |
|---|
要运行此示例,请参见生成具有静态 Windows Phone TextBlock 控件的示例。 |
using System; public sealed class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // ArgumentException is not thrown because 10 is an even number. outputBlock.Text += String.Format("10 divided by 2 is {0}", DivideByTwo(10)) + "\n"; try { // ArgumentException is thrown because 7 is not an even number. outputBlock.Text += String.Format("7 divided by 2 is {0}", DivideByTwo(7)) + "\n"; } catch (ArgumentException) { // Show the user that 7 cannot be divided by 2. outputBlock.Text += "7 is not divided by 2 integrally." + "\n"; } } 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; } } // This code produces the following output. // // 10 divided by 2 is 5 // 7 is not divided by 2 integrally.



注意: