例外のトラブルシューティング : System.ArgumentException

ArgumentException 例外は、メソッドに渡された引数の少なくとも 1 つが、メソッドのパラメーターの仕様に一致していない場合にスローされます。

次の例では、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」を参照してください。 その他のコンストラクターの一覧については、「#ctor」を参照してください。

参照

処理手順

方法: 例外処理アシスタントを使用する

関連項目

ArgumentException