共用方式為


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 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 識別無效參數。

最常見的是,an ArgumentException 是由通用語言執行時或其他類別函式庫拋出,表示開發者錯誤。 如果你從程式碼中拋出 , ArgumentException 應確保該例外 Message 的屬性包含一個有意義的錯誤訊息,描述無效參數及其參數的期望值範圍。

的主要 ArgumentException 導出類別為 ArgumentNullExceptionArgumentOutOfRangeException。 這些導出類別應取代 ArgumentException,除非兩種衍生類別都不可接受。 例如,例外應由以下方式拋出:

如果方法呼叫沒有任何參數,或失敗不涉及參數本身,則 InvalidOperationException 應使用該方法。

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

如需查看 ArgumentException 實例的初始屬性值列表,請參閱 ArgumentException 的建構子。

在 F# 中,你可以使用 invalidArg 函式來產生並提出 ArgumentException。

建構函式

名稱 Description
ArgumentException()

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

ArgumentException(SerializationInfo, StreamingContext)
已淘汰.

使用串行化數據,初始化 ArgumentException 類別的新實例。

ArgumentException(String, Exception)

初始化類別的新實例 ArgumentException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

ArgumentException(String, String, Exception)

初始化類別的新實例 ArgumentException ,包含指定的錯誤訊息、參數名稱,以及對導致此異常的內部例外的參考。

ArgumentException(String, String)

初始化一個新的類別實例 ArgumentException ,並以指定的錯誤訊息及引起此例外的參數名稱。

ArgumentException(String)

初始化類別的新實例 ArgumentException 並指定錯誤訊息。

屬性

名稱 Description
Data

取得一組鍵值對,提供關於例外的額外使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與此例外相關的說明檔案連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,一個編碼的數值,指派給特定例外。

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

會收到錯誤訊息和參數名稱,或者如果沒有設定參數名稱,則只會收到錯誤訊息。

ParamName

取得導致此例外的參數名稱。

Source

取得或設定造成錯誤之應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

會取得呼叫堆疊上即時框架的字串表示。

(繼承來源 Exception)
TargetSite

會取得拋出當前例外的方法。

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

以參數名稱及額外例外資訊設定物件。SerializationInfo

GetType()

取得目前實例的執行時型態。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ThrowIfNullOrEmpty(String, String)

argument 為 或 null 為空,則拋出例外。

ThrowIfNullOrWhiteSpace(String, String)

argumentnull、 為空,或僅包含空白字元,則拋出例外。

ToString()

建立並回傳當前例外的字串表示。

(繼承來源 Exception)

事件

名稱 Description
SerializeObjectState
已淘汰.

當例外被序列化以建立包含該例外序列化資料的例外狀態物件時,會發生這種情況。

(繼承來源 Exception)

適用於

另請參閱