ArgumentException 類別

定義

當其中一個提供給方法的引數無效時所擲回的例外狀況。

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
衍生
屬性
實作

範例

下列範例示範如何擲回和攔截 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 當叫用方法,且至少有一個傳遞的引數不符合所呼叫方法的參數規格時,就會擲回 。 屬性 ParamName 會識別不正確引數。

最常見的情況是 Common Language Runtime 或其他類別庫擲回 , ArgumentException 並指出開發人員錯誤。 如果您從程式碼擲回 ArgumentException ,您應該確定例外狀況的 Message 屬性包含有意義的錯誤訊息,描述不正確引數和引數的預期值範圍。

的主要衍生類別 ArgumentExceptionArgumentNullExceptionArgumentOutOfRangeException 。 這些衍生類別應該使用 而不是 ArgumentException ,除非在無法接受任何衍生類別的情況下使用。 例如,例外狀況應該由下列方式擲回:

如果方法呼叫沒有任何引數,或者如果失敗不包含引數本身, InvalidOperationException 則應該使用 。

ArgumentException 會使用 HRESULT COR_E_ARGUMENT,其值為 0x80070057。

如需執行個體的初始屬性值的清單ArgumentException,請參閱ArgumentException建構函式。

在 F# 中,您可以使用 invalidArg 函式來產生及引發 ArgumentException。

建構函式

ArgumentException()

初始化 ArgumentException 類別的新執行個體。

ArgumentException(SerializationInfo, StreamingContext)
已淘汰.

使用序列化資料,初始化 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

取得擲回目前例外狀況的方法。

(繼承來源 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)

如果 argumentnull 或空白,則會擲回例外狀況。

ThrowIfNullOrWhiteSpace(String, String)

如果 argumentnull 、空白或只包含空白字元,則會擲回例外狀況。

ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱