Share via


예외 문제 해결: System.ArgumentException

ArgumentException 예외는 메서드에 제공된 인수 중 메서드 매개 변수의 사양에 맞지 않는 인수가 최소한 하나 이상 있을 때 throw됩니다.

다음 예제에서는 DivideByTwo 메서드에 전달된 인수가 짝수가 아니면 예외가 throw됩니다.

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 클래스의 모든 인스턴스에는 유효하지 않은 인수 및 사용 가능한 값 범위를 지정하는 정보가 포함되어 있어야 합니다. ArgumentNullException 또는 ArgumentOutOfRangeException 등의 보다 구체적인 예외가 상황을 정확히 설명하는 경우 ArgumentException 대신 이러한 예외를 사용해야 합니다.

다른 언어로 작성된 예제를 비롯하여 이 예제에 대한 자세한 내용은 ArgumentException을 참조하십시오. 추가 생성자의 목록을 보려면 ArgumentException()을 참조하십시오.

참고 항목

작업

방법: 예외 도우미 사용

참조

ArgumentException