閱讀英文

共用方式為


ArgumentOutOfRangeException 類別

定義

當引數值超出叫用方法所定義之值的容許範圍時,所擲回的例外狀況。

C#
public class ArgumentOutOfRangeException : ArgumentException
C#
[System.Serializable]
public class ArgumentOutOfRangeException : ArgumentException
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArgumentOutOfRangeException : ArgumentException
繼承
ArgumentOutOfRangeException
繼承
ArgumentOutOfRangeException
屬性
實作

範例

下列範例會定義 類別,以包含受邀來賓的相關資訊。 如果來賓小於 21,則會 ArgumentOutOfRangeException 擲回例外狀況。

C#
using System;
using static System.Console;

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            var guest1 = new Guest("Ben", "Miller", 17);
            WriteLine(guest1.GuestInfo);
        }
        catch (ArgumentOutOfRangeException argumentOutOfRangeException)
        {
            WriteLine($"Error: {argumentOutOfRangeException.Message}");
        }
    }
}

class Guest
{
    private const int minimumRequiredAge = 21;

    private string firstName;
    private string lastName;
    private int age;

    public Guest(string firstName, string lastName, int age)
    {
        if (age < minimumRequiredAge)
            throw new ArgumentOutOfRangeException(nameof(age), $"All guests must be {minimumRequiredAge}-years-old or older.");

        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public string GuestInfo => $"{firstName} {lastName}, {age}";
}

備註

ArgumentOutOfRangeException叫用方法且至少傳遞至方法的其中一個引數不是 null ,且包含不正確值,不是引數所預期值集合的成員時,就會擲回例外狀況。 屬性 ParamName 會識別不正確引數, ActualValue 如果存在值,則屬性會識別不正確值。

一般而言, ArgumentOutOfRangeException 開發人員錯誤的結果。 您應該消除例外狀況的原因,而不是在區塊中 try/catch 處理例外狀況,或者,如果在將引數傳遞至擲回例外狀況的方法之前,由使用者呼叫或輸入傳回引數,您應該先驗證引數再傳遞至 方法。

ArgumentOutOfRangeException 廣泛使用方式如下:

擲回例外狀況的條件 ArgumentOutOfRangeException 包括:

  • 您正在依集合的索引編號擷取集合的成員,而且索引編號無效。

    這是例外狀況最常見的 ArgumentOutOfRangeException 原因。 一般而言,索引編號無效,原因有四個:

    1. 集合沒有成員,而且您的程式碼會假設它確實有。 下列範例會嘗試擷取沒有專案之集合的第一個專案:

      C#
      using System;
      using System.Collections.Generic;
      
      public class Example4
      {
         public static void Main()
         {
            var list = new List<string>();
            Console.WriteLine("Number of items: {0}", list.Count);
            try {
               Console.WriteLine("The first item: '{0}'", list[0]);
            }
            catch (ArgumentOutOfRangeException e) {
               Console.WriteLine(e.Message);
            }
         }
      }
      // The example displays the following output:
      //   Number of items: 0
      //   Index was out of range. Must be non-negative and less than the size of the collection.
      //   Parameter name: index
      

      若要防止例外狀況,請先檢查集合的 Count 屬性是否大於零,再嘗試擷取任何成員,如下列程式碼片段所示。

      C#
      if (list.Count > 0)
         Console.WriteLine("The first item: '{0}'", list[0]);
      
    2. 在某些情況下,可能會發生例外狀況,因為您嘗試使用不存在的索引將成員新增至集合,而不是藉由呼叫 方法,例如 Add 基於此目的而存在的 方法。 下列範例會嘗試使用不存在的索引,而不是呼叫 List<T>.Add 方法,將專案加入至集合。

      C#
      using System;
      using System.Collections.Generic;
      
      public class Example13
      {
         public static void Main()
         {
            var numbers = new List<int>();
            numbers.AddRange( new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 } );
      
            var squares = new List<int>();
            for (int ctr = 0; ctr < numbers.Count; ctr++)
               squares[ctr] = (int) Math.Pow(numbers[ctr], 2);
         }
      }
      // The example displays the following output:
      //    Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
      //    Parameter name: index
      //       at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
      //       at Example.Main()
      

      下列程式碼片段會更正此錯誤:

      C#
      var squares = new List<int>();
      for (int ctr = 0; ctr < numbers.Count; ctr++)
         squares.Add((int) Math.Pow(numbers[ctr], 2));
      
    3. 您嘗試擷取其索引為負數的專案。 這通常會發生,因為您已搜尋特定專案的索引集合,並錯誤地假設搜尋成功。 在下列範例中,方法的呼叫 List<T>.FindIndex(Predicate<T>) 找不到等於 「Z」 的字串,因此會傳回 -1。 不過,這是不正確索引值。

      C#
      using System;
      using System.Collections.Generic;
      
      public class Example
      {
         public static void Main()
         {
            var list = new List<string>();
            list.AddRange( new String[] { "A", "B", "C" } );
            // Get the index of the element whose value is "Z".
            int index = list.FindIndex((new StringSearcher("Z")).FindEquals);
            try {
               Console.WriteLine("Index {0} contains '{1}'", index, list[index]);
            }
            catch (ArgumentOutOfRangeException e) {
               Console.WriteLine(e.Message);
            }
         }
      }
      
      internal class StringSearcher
      {
         string value;
      
         public StringSearcher(string value)
         {
            this.value = value;
         }
      
         public bool FindEquals(string s)
         {
            return s.Equals(value, StringComparison.InvariantCulture);
         }
      }
      // The example displays the following output:
      //   Index was out of range. Must be non-negative and less than the size of the collection.
      //   Parameter name: index
      

      若要防止例外狀況,請確定傳回的索引大於或等於零,再嘗試從集合擷取專案,如下列程式碼片段所示,檢查搜尋是否成功。

      C#
      // Get the index of the element whose value is "Z".
      int index = list.FindIndex((new StringSearcher("Z")).FindEquals);
      if (index >= 0)
         Console.WriteLine("'Z' is found at index {0}", list[index]);
      
    4. 您嘗試擷取索引等於集合 Count 屬性值的專案,如下列範例所示。

      C#
      using System;
      using System.Collections.Generic;
      
      public class Example8
      {
         public static void Main()
         {
            var list = new List<string>();
            list.AddRange( new String[] { "A", "B", "C" } );
            try {
               // Display the elements in the list by index.
               for (int ctr = 0; ctr <= list.Count; ctr++)
                  Console.WriteLine("Index {0}: {1}", ctr, list[ctr]);
            }
            catch (ArgumentOutOfRangeException e) {
               Console.WriteLine(e.Message);
            }
         }
      }
      // The example displays the following output:
      //   Index 0: A
      //   Index 1: B
      //   Index 2: C
      //   Index was out of range. Must be non-negative and less than the size of the collection.
      //   Parameter name: index
      

      因為 .NET 中的集合使用以零起始的索引編制,所以集合的第一個元素位於索引 0,而最後一個元素位於 index Count - 1。 您可以藉由確保您存取 index Count - 1 的最後一個專案,藉此消除錯誤,如下列程式碼所示。

      C#
      // Display the elements in the list by index.
      for (int ctr = 0; ctr < list.Count; ctr++)
         Console.WriteLine("Index {0}: {1}", ctr, list[ctr]);
      
  • 您嘗試藉由呼叫字串操作方法來執行字串作業,而起始索引不存在於字串中。

    方法的多載,例如 String.CompareString.CompareOrdinalString.IndexOfString.InsertIndexOfAnyString.LastIndexOfString.LastIndexOfAny 、、、 RemoveString.Substring ,可讓您指定作業的起始索引,要求索引必須是字串內的有效位置。 有效的索引範圍從 0 到 String.Length - 1。

    ArgumentOutOfRangeException 例外狀況有四個常見原因:

    1. 您正在使用空字串或 String.Empty 。 因為其 String.Length 屬性會傳回 0,所以任何依索引操作它的嘗試都會 ArgumentOutOfRangeException 擲回例外狀況。 下列範例會 GetFirstCharacter 定義傳回字串第一個字元的方法。 如果字串是空的,因為傳遞至方法的最後一個 ArgumentOutOfRangeException 字串是 ,此方法會擲回例外狀況。

      C#
      using System;
      
      public class Example1
      {
          public static void Main()
          {
              String[] words = { "the", "today", "tomorrow", " ", "" };
              foreach (var word in words)
                  Console.WriteLine("First character of '{0}': '{1}'",
                                    word, GetFirstCharacter(word));
          }
      
          private static char GetFirstCharacter(string s)
          {
              return s[0];
          }
      }
      // The example displays the following output:
      //    First character of //the//: //t//
      //    First character of //today//: //t//
      //    First character of //tomorrow//: //t//
      //    First character of // //: // //
      //
      //    Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
      //       at Example.Main()
      

      您可以藉由測試字串 String.Length 是否大於零或呼叫 IsNullOrEmpty 方法來確保字串不是 null 或空白,來消除例外狀況。 下列程式碼片段會執行後者。 在此情況下,如果字串為 null 或空白,則方法會 GetFirstCharacter 傳回 U+0000。

      C#
      static char GetFirstCharacter(string s)
      {
          if (string.IsNullOrEmpty(s))
              return '\u0000';
          else
              return s[0];
      }
      
    2. 您正在根據該字串內子字串的位置操作字串,而且無法判斷是否確實找到子字串。

      下列範例會擷取雙字片語的第二個字。 如果片語只包含一個 ArgumentOutOfRangeException 單字,因此不包含內嵌空白字元,則會擲回例外狀況。 這是因為對 方法的 String.IndexOf(String) 呼叫會傳回 -1,表示搜尋失敗,然後這個不正確值會傳遞至 String.Substring(Int32) 方法。

      C#
      using System;
      
      public class Example17
      {
         public static void Main()
         {
            String[] phrases = { "ocean blue", "concerned citizen",
                                 "runOnPhrase" };
            foreach (var phrase in phrases)
               Console.WriteLine("Second word is {0}", GetSecondWord(phrase));
         }
      
         static string GetSecondWord(string s)
         {
            int pos = s.IndexOf(" ");
            return s.Substring(pos).Trim();
         }
      }
      // The example displays the following output:
      //    Second word is blue
      //    Second word is citizen
      //
      //    Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
      //    Parameter name: startIndex
      //       at System.String.Substring(Int32 startIndex, Int32 length)
      //       at Example17.GetSecondWord(String s)
      //       at Example17.Main()
      

      若要排除例外狀況,請先驗證字串搜尋方法傳回的值,再呼叫字串操作方法。

      C#
      using System;
      
      public class Example18
      {
         public static void Main()
         {
            String[] phrases = { "ocean blue", "concerned citizen",
                                 "runOnPhrase" };
            foreach (var phrase in phrases) {
               string word = GetSecondWord(phrase);
               if (! string.IsNullOrEmpty(word))
                  Console.WriteLine("Second word is {0}", word);
            }
         }
      
         static string GetSecondWord(string s)
         {
            int pos = s.IndexOf(" ");
            if (pos >= 0)
               return s.Substring(pos).Trim();
            else
               return string.Empty;
         }
      }
      // The example displays the following output:
      //       Second word is blue
      //       Second word is citizen
      
    3. 您已嘗試擷取超出目前字串範圍的子字串。

      擷取子字串的方法全都需要您指定子字串的開始位置,而對於不繼續到字串結尾的子字串,則為子字串中的字元數。 請注意,這不是子字串中最後一個字元的 索引

      ArgumentOutOfRangeException在此情況下,通常會擲回例外狀況,因為您不正確地計運算元字串中的字元數。 如果您使用類似 String.IndexOf 的搜尋方法來識別子字串的開始和結束位置:

      • 如果 所 String.IndexOf 傳回之結束位置中的字元要包含在子字串中,則公式會提供子字串的結束位置

        endIndex - startIndex + 1
        
      • 如果 所 String.IndexOf 傳回之結束位置中的字元要從子字串中排除,則公式會提供子字串的結束位置

        endIndex - startIndex
        

        下列範例會 FindWords 定義方法,這個方法會使用 String.IndexOfAny(Char[], Int32) 方法來識別字串中的空白字元和標點符號,並傳回陣列,其中包含在字串中找到的文字。

        C#
        using System;
        using System.Collections.Generic;
        
        public class Example19
        {
           public static void Main()
           {
              string sentence = "This is a simple, short sentence.";
              Console.WriteLine("Words in '{0}':", sentence);
              foreach (var word in FindWords(sentence))
                 Console.WriteLine("   '{0}'", word);
           }
        
           static String[] FindWords(string s)
           {
              int start = 0, end = 0;
              Char[] delimiters = { ' ', '.', ',', ';', ':', '(', ')' };
              var words = new List<string>();
        
              while (end >= 0) {
                 end = s.IndexOfAny(delimiters, start);
                 if (end >= 0) {
                    if (end - start > 0)
                       words.Add(s.Substring(start, end - start));
        
                    start = end + 1;
                 }
                 else {
                    if (start < s.Length - 1)
                       words.Add(s.Substring(start));
                 }
              }
              return words.ToArray();
           }
        }
        // The example displays the following output:
        //       Words in 'This is a simple, short sentence.':
        //          'This'
        //          'is'
        //          'a'
        //          'simple'
        //          'short'
        //          'sentence'
        
  • 您已將負數傳遞給引數的方法,其引數只需要正數和零,或者您已將負數或零傳遞給引數只需要正數的方法。

    例如, Array.CreateInstance(Type, Int32, Int32, Int32) 方法會要求您在二維陣列的每個維度中指定元素數目;每個維度的有效值可以介於 0 到 Int32.MaxValue 之間。 但因為下列範例中的維度引數有負值,所以 方法會 ArgumentOutOfRangeException 擲回例外狀況。

    C#
    using System;
    
    public class Example01
    {
        public static void Main()
        {
            int dimension1 = 10;
            int dimension2 = -1;
            try
            {
                Array arr = Array.CreateInstance(typeof(string),
                                                 dimension1, dimension2);
            }
            catch (ArgumentOutOfRangeException e)
            {
                if (e.ActualValue != null)
                    Console.WriteLine("{0} is an invalid value for {1}: ", e.ActualValue, e.ParamName);
                Console.WriteLine(e.Message);
            }
        }
    }
    // The example displays the following output:
    //     Non-negative number required.
    //     Parameter name: length2
    

    若要更正錯誤,請確定無效引數的值不是負數。 您可以藉由提供有效的值來執行此動作,如下列程式碼片段所示。

    C#
    int dimension1 = 10;
    int dimension2 = 10;
    Array arr = Array.CreateInstance(typeof(string),
                                     dimension1, dimension2);
    

    您也可以驗證輸入,如果輸入無效,請採取一些動作。 下列程式碼片段會顯示錯誤訊息,而不是呼叫 方法。

    C#
    if (dimension1 < 0 || dimension2 < 0)
    {
        Console.WriteLine("Unable to create the array.");
        Console.WriteLine("Specify non-negative values for the two dimensions.");
    }
    else
    {
        arr = Array.CreateInstance(typeof(string),
                                   dimension1, dimension2);
    }
    
  • 競爭條件存在於多執行緒或具有非同步執行的工作,以及更新陣列或集合的工作中。

    下列範例會使用 List<T> 物件來填入 物件的集合 Continent 。 如果範例嘗試在完整填入集合之前顯示集合中的七個專案,則會擲 ArgumentOutOfRangeException 回 。

    C#
    using System;
    using System.Collections.Generic;
    using System.Threading;
    
    public class Continent
    {
       public string? Name { get; set; }
       public int Population { get; set; }
       public Decimal Area { get; set; }
    }
    
    public class Example11
    {
       static List<Continent> continents = new List<Continent>();
       static string? s_msg;
    
       public static void Main()
       {
          String[] names = { "Africa", "Antarctica", "Asia",
                             "Australia", "Europe", "North America",
                             "South America" };
          // Populate the list.
          foreach (var name in names) {
             var th = new Thread(PopulateContinents);
             th.Start(name);
          }
          Console.WriteLine(s_msg);
          Console.WriteLine();
    
          // Display the list.
          for (int ctr = 0; ctr < names.Length; ctr++) {
             var continent = continents[ctr];
             Console.WriteLine("{0}: Area: {1}, Population {2}",
                               continent.Name, continent.Population,
                               continent.Area);
          }
       }
    
       private static void PopulateContinents(Object? obj)
       {
          string? name = obj?.ToString();
          s_msg += string.Format("Adding '{0}' to the list.\n", name);
          var continent = new Continent();
          continent.Name = name;
          // Sleep to simulate retrieving remaining data.
          Thread.Sleep(50);
          continents.Add(continent);
       }
    }
    // The example displays output like the following:
    //    Adding //Africa// to the list.
    //    Adding //Antarctica// to the list.
    //    Adding //Asia// to the list.
    //    Adding //Australia// to the list.
    //    Adding //Europe// to the list.
    //    Adding //North America// to the list.
    //    Adding //South America// to the list.
    //
    //
    //
    //    Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    //    Parameter name: index
    //       at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
    //       at Example.Main()
    

    在此情況下,會從多個執行緒存取兩個資源:

    • continents 集合。 從多個執行緒呼叫其 List<T>.Add 方法。 此外,主執行緒或主要執行緒會假設集合在逐一查看其成員時,會完全填入七個元素。

    • msg字串,從多個執行緒串連。

    若要更正錯誤,請確定以安全線程的方式存取共用狀態,如下所示。

    • 如果您的應用程式使用陣列或集合物件,請考慮使用安全線程集合類別,例如命名空間中的 System.Collections.Concurrent 類型或 System.Collections.Immutable 頻外版本。

    • 請確定共用狀態 (也就是說,多個執行緒可以存取的資源) 會以安全線程的方式存取,因此一次只有一個執行緒具有資源的獨佔存取權。 有許多類別,例如 CountdownEventInterlockedMonitor 、 和 Mutex ,可用來同步存取資源。 如需詳細資訊,請參閱 執行緒。 此外,語言支援可透過 C# 中的 lock 語句和 Visual Basic 中的 SyncLock 建構來取得。

    下列範例會解決 ArgumentOutOfRangeException 上一個範例中的 和 其他問題。 它會將 物件取代 List<T>ConcurrentBag<T> 物件,以確保集合的存取是安全線程、使用 CountdownEvent 物件來確保只有在執行其他執行緒之後,應用程式執行緒才會繼續,並使用鎖定來確保一次只能有一個執行緒存取 msg 變數。

    C#
    using System;
    using System.Collections.Concurrent;
    using System.Threading;
    
    public class ContinentD
    {
       public string? Name { get; set; }
       public int Population { get; set; }
       public Decimal Area { get; set; }
    }
    
    public class Example12
    {
       static ConcurrentBag<ContinentD> ContinentDs = new ConcurrentBag<ContinentD>();
       static CountdownEvent? gate;
       static string msg = string.Empty;
    
       public static void Main()
       {
          String[] names = { "Africa", "Antarctica", "Asia",
                             "Australia", "Europe", "North America",
                             "South America" };
          gate = new CountdownEvent(names.Length);
    
          // Populate the list.
          foreach (var name in names) {
             var th = new Thread(PopulateContinentDs);
             th.Start(name);
          }
    
          // Display the list.
          gate.Wait();
          Console.WriteLine(msg);
          Console.WriteLine();
    
          var arr = ContinentDs.ToArray();
          for (int ctr = 0; ctr < names.Length; ctr++) {
             var ContinentD = arr[ctr];
             Console.WriteLine("{0}: Area: {1}, Population {2}",
                               ContinentD.Name, ContinentD.Population,
                               ContinentD.Area);
          }
       }
    
       private static void PopulateContinentDs(Object? obj)
       {
          string? name = obj?.ToString();
          lock(msg) {
             msg += string.Format("Adding '{0}' to the list.\n", name);
          }
          var ContinentD = new ContinentD();
          ContinentD.Name = name;
          // Sleep to simulate retrieving remaining data.
          Thread.Sleep(25);
          ContinentDs.Add(ContinentD);
          gate?.Signal();
       }
    }
    // The example displays output like the following:
    //       Adding 'Africa' to the list.
    //       Adding 'Antarctica' to the list.
    //       Adding 'Asia' to the list.
    //       Adding 'Australia' to the list.
    //       Adding 'Europe' to the list.
    //       Adding 'North America' to the list.
    //       Adding 'South America' to the list.
    //
    //
    //       Africa: Area: 0, Population 0
    //       Antarctica: Area: 0, Population 0
    //       Asia: Area: 0, Population 0
    //       Australia: Area: 0, Population 0
    //       Europe: Area: 0, Population 0
    //       North America: Area: 0, Population 0
    //       South America: Area: 0, Population 0
    

ArgumentOutOfRangeException 會使用 HRESULT COR_E_ARGUMENTOUTOFRANGE,其值為 0x80131502。

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

建構函式

ArgumentOutOfRangeException()

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

ArgumentOutOfRangeException(SerializationInfo, StreamingContext)
已淘汰.

使用序列化資料,初始化 ArgumentOutOfRangeException 類別的新執行個體。

ArgumentOutOfRangeException(String)

使用造成這個例外狀況的參數名稱來初始化 ArgumentOutOfRangeException 類別的新執行個體。

ArgumentOutOfRangeException(String, Exception)

使用指定的錯誤訊息和造成這個例外狀況的例外狀況,初始化 ArgumentOutOfRangeException 類別的新執行個體。

ArgumentOutOfRangeException(String, Object, String)

使用參數名稱引數值和指定的錯誤訊息,初始化 ArgumentOutOfRangeException 類別的新執行個體。

ArgumentOutOfRangeException(String, String)

使用造成這個例外狀況的參數名稱和指定的錯誤訊息,初始化 ArgumentOutOfRangeException 類別的新執行個體。

屬性

ActualValue

取得造成這個例外狀況的引數值。

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得錯誤訊息和無效引數值的字串表示,或如果引數值是 null 時,只取得錯誤訊息。

ParamName

取得造成這個例外狀況的參數名稱。

(繼承來源 ArgumentException)
Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

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

(繼承來源 Exception)

方法

Equals(Object)

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

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

以無效的引數值和其他例外狀況資訊設定 SerializationInfo 物件。

GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

設定具有參數名稱和額外例外狀況資訊的 SerializationInfo 物件。

(繼承來源 ArgumentException)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ThrowIfEqual<T>(T, T, String)

如果 value 等於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfGreaterThan<T>(T, T, String)

如果 value 大於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfGreaterThanOrEqual<T>(T, T, String)

如果 value 大於或等於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfLessThan<T>(T, T, String)

如果 value 小於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfLessThanOrEqual<T>(T, T, String)

如果 value 小於或等於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfNegative<T>(T, String)

如果 value 為負數,則會 ArgumentOutOfRangeException 擲回 。

ThrowIfNegativeOrZero<T>(T, String)

如果 value 為負數或零,則會 ArgumentOutOfRangeException 擲回 。

ThrowIfNotEqual<T>(T, T, String)

如果 value 不等於 other ,則會擲回 ArgumentOutOfRangeException

ThrowIfZero<T>(T, String)

如果 value 為零,則會擲回 ArgumentOutOfRangeException

ToString()

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

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

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

(繼承來源 Exception)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱