StackOverflowException 類別

定義

例外狀況,擲回於執行堆疊因為包含過多巢狀方法呼叫而溢位時。The exception that is thrown when the execution stack overflows because it contains too many nested method calls. 無法繼承這個類別。This class cannot be inherited.

public ref class StackOverflowException sealed : SystemException
public sealed class StackOverflowException : SystemException
[System.Serializable]
public sealed class StackOverflowException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class StackOverflowException : SystemException
type StackOverflowException = class
    inherit SystemException
Public NotInheritable Class StackOverflowException
Inherits SystemException
繼承
StackOverflowException
屬性

範例

下列範例會使用計數器,以確保 Execute 方法的遞迴呼叫數目不會超過 MAX_RECURSIVE_CALLS 常數所定義的最大值。The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant.

using System;

public class Example
{
   private const int MAX_RECURSIVE_CALLS = 1000;
   static int ctr = 0;
   
   public static void Main()
   {
      Example ex = new Example();
      ex.Execute();
      Console.WriteLine("\nThe call counter: {0}", ctr);
   }

   private void Execute()
   {
      ctr++;
      if (ctr % 50 == 0)
         Console.WriteLine("Call number {0} to the Execute method", ctr);
         
      if (ctr <= MAX_RECURSIVE_CALLS)
         Execute();
         
      ctr--;
   }
}
// The example displays the following output:
//       Call number 50 to the Execute method
//       Call number 100 to the Execute method
//       Call number 150 to the Execute method
//       Call number 200 to the Execute method
//       Call number 250 to the Execute method
//       Call number 300 to the Execute method
//       Call number 350 to the Execute method
//       Call number 400 to the Execute method
//       Call number 450 to the Execute method
//       Call number 500 to the Execute method
//       Call number 550 to the Execute method
//       Call number 600 to the Execute method
//       Call number 650 to the Execute method
//       Call number 700 to the Execute method
//       Call number 750 to the Execute method
//       Call number 800 to the Execute method
//       Call number 850 to the Execute method
//       Call number 900 to the Execute method
//       Call number 950 to the Execute method
//       Call number 1000 to the Execute method
//
//       The call counter: 0
Module Example
   Private Const MAX_RECURSIVE_CALLS As Integer = 1000
   Dim ctr As Integer = 0

   Public Sub Main()
      Execute()
      Console.WriteLine()
      Console.WriteLine("The call counter: {0}", ctr)
   End Sub

   Private Sub Execute()
      ctr += 1
      If ctr Mod 50 = 0 Then
         Console.WriteLine("Call number {0} to the Execute method", ctr)
      End If
      
      If ctr <= MAX_RECURSIVE_CALLS Then
         Execute()
      End If

      ctr -= 1
   End Sub
End Module
' The example displays the following output:
'       Call number 50 to the Execute method
'       Call number 100 to the Execute method
'       Call number 150 to the Execute method
'       Call number 200 to the Execute method
'       Call number 250 to the Execute method
'       Call number 300 to the Execute method
'       Call number 350 to the Execute method
'       Call number 400 to the Execute method
'       Call number 450 to the Execute method
'       Call number 500 to the Execute method
'       Call number 550 to the Execute method
'       Call number 600 to the Execute method
'       Call number 650 to the Execute method
'       Call number 700 to the Execute method
'       Call number 750 to the Execute method
'       Call number 800 to the Execute method
'       Call number 850 to the Execute method
'       Call number 900 to the Execute method
'       Call number 950 to the Execute method
'       Call number 1000 to the Execute method
'
'       The call counter: 0

備註

執行堆疊溢位錯誤會擲回 StackOverflowException,通常是發生非常深或未系結的遞迴。StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. 因此,請確定您的程式碼沒有無限迴圈或無限遞迴。So make sure your code doesn't have an infinite loop or infinite recursion.

StackOverflowException 使用 HRESULT COR_E_STACKOVERFLOW,其值為0x800703E9。StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9. Localloc 的中繼語言(IL)指令會擲回 StackOverflowExceptionThe Localloc intermediate language (IL) instruction throws StackOverflowException. 如需 StackOverflowException 物件的初始屬性值清單,請參閱 StackOverflowException 的函式。For a list of initial property values for a StackOverflowException object, see the StackOverflowException constructors.

從 .NET Framework 2.0 開始,您無法使用 try/catch 區塊來攔截 StackOverflowException 物件,而且依預設會終止對應的進程。Starting with the .NET Framework 2.0, you can't catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default. 因此,您應該撰寫程式碼來偵測並防止堆疊溢位。Consequently, you should write your code to detect and prevent a stack overflow. 例如,如果您的應用程式相依于遞迴,請使用計數器或狀態條件來終止遞迴迴圈。For example, if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. 如需這項技術的圖例,請參閱範例一節。See the Examples section for an illustration of this technique.

注意

HandleProcessCorruptedStateExceptionsAttribute 屬性套用至擲回 StackOverflowException 的方法不會有任何作用。Applying the HandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws a StackOverflowException has no effect. 您仍然無法從使用者程式碼處理例外狀況。You still cannot handle the exception from user code.

如果您的應用程式裝載 common language runtime (CLR),它可以指定 CLR 應該卸載發生堆疊溢位例外狀況的應用程式域,並讓對應的進程繼續進行。If your app hosts the common language runtime (CLR), it can specify that the CLR should unload the application domain where the stack overflow exception occurs and let the corresponding process continue. 如需詳細資訊,請參閱ICLRPolicyManager 介面For more information, see ICLRPolicyManager Interface.

建構函式

StackOverflowException()

初始化 StackOverflowException 類別的新執行個體,設定新執行個體的 Message 屬性為描述錯誤的系統提供訊息,例如「要求的作業造成堆疊溢位」。Initializes a new instance of the StackOverflowException class, setting the Message property of the new instance to a system-supplied message that describes the error, such as "The requested operation caused a stack overflow." 這個訊息會考量目前系統的文化特性。This message takes into account the current system culture.

StackOverflowException(String)

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

StackOverflowException(String, Exception)

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

屬性

Data

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

(繼承來源 Exception)
HelpLink

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

(繼承來源 Exception)
HResult

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

(繼承來源 Exception)
InnerException

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

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(繼承來源 Exception)
Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

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

(繼承來源 Exception)

方法

Equals(Object)

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

(繼承來源 Object)
GetBaseException()

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

(繼承來源 Exception)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(繼承來源 Exception)
GetType()

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

(繼承來源 Exception)
MemberwiseClone()

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

(繼承來源 Object)
ToString()

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

(繼承來源 Exception)

事件

SerializeObjectState

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

(繼承來源 Exception)

適用於

另請參閱