StackOverflowException StackOverflowException StackOverflowException StackOverflowException Class

定义

因包含的嵌套方法调用过多而导致执行堆栈溢出时引发的异常。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
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class StackOverflowException : SystemException
type StackOverflowException = class
    inherit SystemException
Public NotInheritable Class StackOverflowException
Inherits SystemException
继承
StackOverflowExceptionStackOverflowExceptionStackOverflowExceptionStackOverflowException
属性

示例

下面的示例使用计数器来确保对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使用 COR_E_STACKOVERFLOW 值为0x800703E9 的 HRESULT。StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9. 中间语言 (IL) 指令引发StackOverflowExceptionLocallocThe 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 StackOverflowException开始, 不能try / catch使用块捕获对象, 并且在默认情况下会终止相应的进程。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.

备注

将特性应用于引发的StackOverflowException方法不起作用。 HandleProcessCorruptedStateExceptionsAttributeApplying the HandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws a StackOverflowException has no effect. 你仍无法处理用户代码中的异常。You still cannot handle the exception from user code.

如果你的应用托管公共语言运行时 (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() StackOverflowException() 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(String) StackOverflowException(String) StackOverflowException(String)

用指定的错误消息初始化 StackOverflowException 类的新实例。Initializes a new instance of the StackOverflowException class with a specified error message.

StackOverflowException(String, Exception) StackOverflowException(String, Exception) StackOverflowException(String, Exception) 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 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 a message that describes the current exception.

(Inherited from 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()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When 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)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
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)

适用于

另请参阅