关于异常的疑难解答:System.ArgumentException

当提供给某个方法的实参中至少有一个不符合该方法的形参规范时,将引发 ArgumentException 异常。

在下面的示例中,当发送给方法 DivideByTwo 的参数不是偶数时会引发异常。

Module Module1

    Sub Main()

        ' ArgumentException is not thrown in DivideByTwo because 10 is 
        ' an even number.
        Console.WriteLine("10 divided by 2 is {0}", DivideByTwo(10))
        Try
            ' ArgumentException is thrown in DivideByTwo because 7 is 
            ' not an even number.
            Console.WriteLine("7 divided by 2 is {0}", DivideByTwo(7))
        Catch argEx As ArgumentException
            ' Tell the user which problem is encountered.
            Console.WriteLine("7 cannot be evenly divided by 2.")
            Console.WriteLine("Exception message: " & argEx.Message)
        End Try
        ' Uncomment the next statement to see what happens if you call 
        ' DivideByTwo directly.
        'Console.WriteLine(DivideByTwo(7))

    End Sub

    Function DivideByTwo(ByVal num As Integer) As Integer

        ' If num is an odd number, throw an ArgumentException. The
        ' ArgumentException class provides a number of constructors
        ' that you can choose from.
        If num Mod 2 = 1 Then
            Throw New ArgumentException("Argument for num must be" & _
                " an even number.")
        End If

        ' Value of num is even, so return half of its value.
        Return num / 2

    End Function

End Module

ArgumentException 类的所有实例都应包含用于指定无效的参数以及可接受值的范围的信息。 如果某个更加精确的异常(如 ArgumentNullExceptionArgumentOutOfRangeException)可准确地描述情况,则应使用该异常而不是 ArgumentException。

有关此异常的更多信息(包括其他语言的示例),请参见 ArgumentException。 有关其他构造函数的列表,请参见 ArgumentException()。

请参见

任务

如何:使用异常助手

参考

ArgumentException