Troubleshooting Exceptions: System.ArgumentException

An ArgumentException exception is thrown when at least one of the arguments provided to a method does not meet the specifications of the parameters of the method.

In the following example, the exception is thrown when the argument sent to method DivideByTwo is not an even number.

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

All instances of the ArgumentException class should include information that specifies which argument is not valid, and what the range of acceptable values is. If a more precise exception, such as ArgumentNullException or ArgumentOutOfRangeException, accurately describes the situation, it should be used instead of ArgumentException.

For more information about this exception, including examples in other languages, see ArgumentException. For a list of additional constructors, see ArgumentException().

See Also

Tasks

How to: Use the Exception Assistant

Reference

ArgumentException