ArgumentException 클래스

정의

메서드에 제공된 인수 중 하나가 유효하지 않을 때 throw되는 예외입니다.

public ref class ArgumentException : Exception
public ref class ArgumentException : SystemException
public class ArgumentException : Exception
public class ArgumentException : SystemException
[System.Serializable]
public class ArgumentException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArgumentException : SystemException
type ArgumentException = class
    inherit Exception
type ArgumentException = class
    inherit SystemException
type ArgumentException = class
    inherit SystemException
    interface ISerializable
[<System.Serializable>]
type ArgumentException = class
    inherit SystemException
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArgumentException = class
    inherit SystemException
    interface ISerializable
Public Class ArgumentException
Inherits Exception
Public Class ArgumentException
Inherits SystemException
상속
ArgumentException
상속
ArgumentException
파생
특성
구현

예제

다음 예제에서는 throw 하 고 catch 하는 방법을 보여 줍니다.ArgumentException ArgumentException.GetType()을 사용합니다. Name 속성은 예외 개체의 이름을 표시하고 속성을 사용하여 Message 예외 메시지의 텍스트를 표시합니다.

using namespace System;

static int DivideByTwo(int num) 
{
    // If num is an odd number, throw an ArgumentException.
    if ((num & 1) == 1)
        throw gcnew ArgumentException(String::Format("{0} is not an even number", num), 
                                      "num");

    // num is even, return half of its value.
    return num / 2;
}

void main() 
{
    // Define some integers for a division operation.
    array<int>^ values = { 10, 7 };
    for each (int value in values) {
        try {
           Console::WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value));
        }
        catch (ArgumentException^ e) {
           Console::WriteLine("{0}: {1}", e->GetType()->Name, e->Message);
        }
        Console::WriteLine();
    }
}
// This example displays the following output:
//     10 divided by 2 is 5
//     
//     ArgumentException: 7 is not an even number
//     Parameter name: num
using System;

public class Example
{
    static void Main()
    {
        // Define some integers for a division operation.
        int[] values = { 10, 7 };
        foreach (var value in values) {
            try {
               Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value));
            }
            catch (ArgumentException e) {
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
            }
            Console.WriteLine();
        }
    }

    static int DivideByTwo(int num)
    {
        // If num is an odd number, throw an ArgumentException.
        if ((num & 1) == 1)
            throw new ArgumentException(String.Format("{0} is not an even number", num),
                                      "num");

        // num is even, return half of its value.
        return num / 2;
    }
}
// This example displays the following output:
//     10 divided by 2 is 5
//
//     ArgumentException: 7 is not an even number
//     Parameter name: num
open System

let divideByTwo num =
    // If num is an odd number, throw an ArgumentException.
    if num % 2 = 1 then
        invalidArg "num" $"{num} is not an even number"

    // num is even, return half of its value.
    num / 2;

// Define some integers for a division operation.
let values = [ 10; 7 ]
for value in values do
    try 
        printfn $"{value} divided by 2 is {divideByTwo value}"
    with
    | :? ArgumentException as e ->
        printfn $"{e.GetType().Name}: {e.Message}"
    
    printfn ""

// This example displays the following output:
//     10 divided by 2 is 5
//
//     ArgumentException: 7 is not an even number (Parameter 'num')
Public Class Example
    Public Shared Sub Main()
        ' Define some integers for a division operation.
        Dim values() As Integer = { 10, 7 }

        For Each value In values
            Try 
               Console.WriteLine("{0} divided by 2 is {1}", value, DivideByTwo(value))
            Catch e As ArgumentException
               Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
            End Try
            Console.WriteLine()
        Next
    End Sub
    
    Private Shared Function DivideByTwo(ByVal num As Integer) As Integer
        ' If num is an odd number, throw an ArgumentException.
        If (num And 1) = 1 Then
            Throw New ArgumentException(String.Format("{0} is not an even number", num), 
                                      "num")
        End If
        Return num \ 2
    End Function
End Class
' The example displays the following output:
'     10 divided by 2 is 5
'     
'     ArgumentException: 7 is not an even number
'     Parameter name: num

설명

ArgumentException 는 메서드가 호출되고 전달된 인수 중 하나 이상이 호출된 메서드의 매개 변수 사양을 충족하지 않을 때 throw됩니다. 속성은 ParamName 잘못된 인수를 식별합니다.

가장 일반적으로 ArgumentException 는 공용 언어 런타임 또는 다른 클래스 라이브러리에 의해 throw되며 개발자 오류를 나타냅니다. 코드에서 를 ArgumentException throw하는 경우 예외의 Message 속성에 잘못된 인수와 인수에 대해 예상되는 값 범위를 설명하는 의미 있는 오류 메시지가 포함되어 있는지 확인해야 합니다.

의 기본 파생 클래스는 ArgumentExceptionArgumentOutOfRangeException입니다ArgumentNullException. 파생 클래스 중 어느 것도 허용되지 않는 경우를 제외하고 이러한 파생 클래스를 대신 사용해야 ArgumentException합니다. 예를 들어 예외는 다음을 통해 throw되어야 합니다.

  • ArgumentNullException 가 유효한 인수로 수락하지 않는 메서드에 전달될 때마다 null 입니다.

  • ArgumentOutOfRangeException 인수의 값이 허용되는 값 범위를 벗어나면 이고, 예를 들어 를 만드는 동안 값 "46"이 month 인수로 전달되는 경우입니다 DateTime.

메서드 호출에 인수가 없거나 오류가 인수 자체를 InvalidOperationException 포함하지 않는 경우 를 사용해야 합니다.

ArgumentException 는 값이 0x80070057 HRESULT COR_E_ARGUMENT 사용합니다.

인스턴스의 초기 속성 값의 목록을 ArgumentException, 참조는 ArgumentException 생성자입니다.

F#에서 invalidArg 함수를 사용하여 ArgumentException을 생성하고 발생할 수 있습니다.

생성자

ArgumentException()

ArgumentException 클래스의 새 인스턴스를 초기화합니다.

ArgumentException(SerializationInfo, StreamingContext)
사용되지 않음.

serialize된 데이터를 사용하여 ArgumentException 클래스의 새 인스턴스를 초기화합니다.

ArgumentException(String)

지정된 오류 메시지를 사용하여 ArgumentException 클래스의 새 인스턴스를 초기화합니다.

ArgumentException(String, Exception)

지정된 오류 메시지와 해당 예외의 원인인 내부 예외에 대한 참조를 사용하여 ArgumentException 클래스의 새 인스턴스를 초기화합니다.

ArgumentException(String, String)

지정된 오류 메시지와 이 예외를 발생한 매개 변수의 이름을 사용하여 ArgumentException 클래스의 새 인스턴스를 초기화합니다.

ArgumentException(String, String, Exception)

지정된 오류 메시지, 매개 변수 이름 및 이 예외의 원인인 내부 예외에 대한 참조를 사용하여 ArgumentException 클래스의 새 인스턴스를 초기화합니다.

속성

Data

예외에 대한 사용자 정의 정보를 추가로 제공하는 키/값 쌍 컬렉션을 가져옵니다.

(다음에서 상속됨 Exception)
HelpLink

이 예외와 연결된 도움말 파일에 대한 링크를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
HResult

특정 예외에 할당된 코드화된 숫자 값인 HRESULT를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
InnerException

현재 예외를 발생시킨 Exception 인스턴스를 가져옵니다.

(다음에서 상속됨 Exception)
Message

오류 메시지 및 매개 변수 이름을 가져오거나, 매개 변수 이름이 설정되지 않은 경우에는 오류 메시지만 가져옵니다.

ParamName

이 예외를 발생시킨 매개 변수의 이름을 가져옵니다.

Source

오류를 발생시키는 애플리케이션 또는 개체의 이름을 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
StackTrace

호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다.

(다음에서 상속됨 Exception)
TargetSite

현재 예외를 throw하는 메서드를 가져옵니다.

(다음에서 상속됨 Exception)

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetBaseException()

파생 클래스에서 재정의된 경우 하나 이상의 후속 예외의 근본 원인이 되는 Exception 을 반환합니다.

(다음에서 상속됨 Exception)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetObjectData(SerializationInfo, StreamingContext)
사용되지 않음.

매개 변수 이름 및 추가 예외 정보를 사용하여 SerializationInfo 개체를 설정합니다.

GetObjectData(SerializationInfo, StreamingContext)
사용되지 않음.

파생 클래스에서 재정의된 경우 예외에 관한 정보를 SerializationInfo 에 설정합니다.

(다음에서 상속됨 Exception)
GetType()

현재 인스턴스의 런타임 형식을 가져옵니다.

(다음에서 상속됨 Exception)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ThrowIfNullOrEmpty(String, String)

가 이거나 비어 있는 경우 argument 예외를 null throw합니다.

ThrowIfNullOrWhiteSpace(String, String)

null, 비어 있거나 공백 문자로만 구성된 경우 argument 예외를 throw합니다.

ToString()

현재 예외에 대한 문자열 표현을 만들고 반환합니다.

(다음에서 상속됨 Exception)

이벤트

SerializeObjectState
사용되지 않음.

예외에 대한 serialize된 데이터가 들어 있는 예외 상태 개체가 만들어지도록 예외가 serialize될 때 발생합니다.

(다음에서 상속됨 Exception)

적용 대상

추가 정보