ArgumentException ArgumentException ArgumentException ArgumentException Class

定義

當其中一個提供給方法的引數無效時所擲回的例外狀況。The exception that is thrown when one of the arguments provided to a method is not valid.

public ref class ArgumentException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class ArgumentException : SystemException
type ArgumentException = class
    inherit SystemException
    interface ISerializable
Public Class ArgumentException
Inherits SystemException
繼承
衍生
屬性
實作

範例

下列範例示範如何擲回並攔截ArgumentExceptionThe following example demonstrates how to throw and catch an ArgumentException. 它會使用ArgumentException.GetType()。名稱屬性來顯示例外狀況物件,以及使用名稱Message屬性以顯示例外狀況訊息的文字。It uses the ArgumentException.GetType().Name property to display the name of the exception object, and also uses the Message property to display the text of the exception 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
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 當叫用方法,並至少一個傳遞的引數不符合呼叫之方法的參數規格時,會擲回。ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. ParamName屬性會識別無效的引數。The ParamName property identifies the invalid argument.

大多數情況下, ArgumentException common language runtime 或另一個類別庫所擲回,並指出開發人員的錯誤。Most commonly, an ArgumentException is thrown by the common language runtime or another class library and indicates developer error. 如果您擲回ArgumentException從您的程式碼中,您應該確保例外狀況的Message屬性包含有意義的錯誤訊息,描述無效的引數和預期的引數的值範圍。If you throw an ArgumentException from your code, you should ensure that the exception's Message property includes a meaningful error message that describes the invalid argument and the expected range of values for the argument.

主要的衍生類別ArgumentExceptionArgumentNullExceptionArgumentOutOfRangeExceptionThe primary derived classes of ArgumentException are ArgumentNullException and ArgumentOutOfRangeException. 應該用於這些衍生的類別,而不是ArgumentException,除非在衍生類別都不是可接受的情況。These derived classes should be used instead of ArgumentException, except in situations where neither of the derived classes is acceptable. 比方說,應該會擲回例外狀況:For example, exceptions should be thrown by:

如果方法呼叫並沒有任何引數,或失敗無關的引數本身,然後InvalidOperationException應該使用。If the method call does not have any argument or if the failure does not involve the arguments themselves, then InvalidOperationException should be used.

ArgumentException 會使用 HRESULT COR_E_ARGUMENT,其值 0x80070057。ArgumentException uses the HRESULT COR_E_ARGUMENT, which has the value 0x80070057.

如需執行個體的初始屬性值的清單ArgumentException,請參閱ArgumentException建構函式。For a list of initial property values for an instance of ArgumentException, see the ArgumentException constructors.

建構函式

ArgumentException() ArgumentException() ArgumentException() ArgumentException()

初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class.

ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext) ArgumentException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class with serialized data.

ArgumentException(String) ArgumentException(String) ArgumentException(String) ArgumentException(String)

使用指定的錯誤訊息,初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class with a specified error message.

ArgumentException(String, Exception) ArgumentException(String, Exception) ArgumentException(String, Exception) ArgumentException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class with a specified error message and a reference to the inner exception that is the cause of this exception.

ArgumentException(String, String) ArgumentException(String, String) ArgumentException(String, String) ArgumentException(String, String)

使用指定的錯誤訊息和造成這個例外狀況的參數名稱,初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class with a specified error message and the name of the parameter that causes this exception.

ArgumentException(String, String, Exception) ArgumentException(String, String, Exception) ArgumentException(String, String, Exception) ArgumentException(String, String, Exception)

使用指定的錯誤訊息、參數名稱和造成這個例外狀況原因的內部例外參考,初始化 ArgumentException 類別的新執行個體。Initializes a new instance of the ArgumentException class with a specified error message, the parameter name, and a reference to the inner exception that is the cause of this exception.

屬性

Data Data Data Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink HelpLink HelpLink HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

取得錯誤訊息和參數名稱,在參數名稱未設定時只會取得錯誤訊息。Gets the error message and the parameter name, or only the error message if no parameter name is set.

ParamName ParamName ParamName ParamName

取得造成這個例外狀況的參數名稱。Gets the name of the parameter that causes this exception.

Source Source Source Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

設定具有參數名稱和額外例外狀況資訊的 SerializationInfo 物件。Sets the SerializationInfo object with the parameter name and additional exception information.

GetType() GetType() GetType() GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

適用於

另請參閱