ArgumentException 类

在向方法提供的其中一个参数无效时引发的异常。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ArgumentException
    Inherits SystemException
    Implements ISerializable
用法
Dim instance As ArgumentException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class ArgumentException : SystemException, ISerializable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class ArgumentException : public SystemException, ISerializable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class ArgumentException extends SystemException implements ISerializable
SerializableAttribute 
ComVisibleAttribute(true) 
public class ArgumentException extends SystemException implements ISerializable

备注

在调用某方法但传递的参数中至少有一个不符合所调用方法的参数规范时,将引发 ArgumentExceptionArgumentException 的所有实例均应带有有意义的错误信息,描述无效参数以及该参数所需的值范围。

ArgumentException 的主要派生类有 ArgumentNullExceptionArgumentOutOfRangeException。应使用这两种派生类取代 ArgumentException,除非这两种派生类都不被接受。例如:

  • 每当向方法传递 空引用(在 Visual Basic 中为 Nothing) 而该方法不把它作为有效参数接受时,应由 ArgumentNullException 引发异常。

  • 当参数值超出可接受值的范围(例如,在创建 DateTime 时将值“46”作为月份参数传递)时,应由 ArgumentOutOfRangeException 引发异常。

如果方法调用没有任何参数,或者失败未涉及参数本身,则应当使用 InvalidOperationException 引发异常。

ArgumentException 使用值为 0x80070057 的 HRESULT COR_E_ARGUMENT。

有关 ArgumentException 实例的初始属性值列表,请参见 ArgumentException 构造函数。

示例

下面的示例演示如何引发和捕捉 ArgumentException

using System;

public sealed class App 
{
    static void Main() 
    {
        // ArgumentException is not thrown because 10 is an even number.
        Console.WriteLine("10 divided by 2 is {0}", DivideByTwo(10));
        try 
        {
             // ArgumentException is thrown because 7 is not an even number.
             Console.WriteLine("7 divided by 2 is {0}", DivideByTwo(7));
        }
        catch (ArgumentException)
        {
            // Show the user that 7 cannot be divided by 2.
            Console.WriteLine("7 is not divided by 2 integrally.");
        }
    }

    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.
using namespace System;

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;
}

int main()
{
    // ArgumentException is not thrown because 10 is an even number.
    Console::WriteLine("10 divided by 2 is {0}", DivideByTwo(10));
    try
    {
        // ArgumentException is thrown because 7 is not an even number.
        Console::WriteLine("7 divided by 2 is {0}", DivideByTwo(7));
    }
    catch (ArgumentException^)
    {
        // Show the user that 7 cannot be divided by 2.
        Console::WriteLine("7 is not divided by 2 integrally.");
    }
}

// This code produces the following output.
//
// 10 divided by 2 is 5
// 7 is not divided by 2 integrally.

继承层次结构

System.Object
   System.Exception
     System.SystemException
      System.ArgumentException
         派生类

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ArgumentException 成员
System 命名空间
Exception

其他资源

处理和引发异常