StackOverflowException 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
执行堆栈超过堆栈大小时引发的异常。 此类不能被继承。
public ref class StackOverflowException sealed : SystemException
	public sealed class StackOverflowException : SystemException
	[System.Serializable]
public sealed class StackOverflowException : SystemException
	[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StackOverflowException : SystemException
	type StackOverflowException = class
    inherit SystemException
	[<System.Serializable>]
type StackOverflowException = class
    inherit SystemException
	[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackOverflowException = class
    inherit SystemException
	Public NotInheritable Class StackOverflowException
Inherits SystemException
		- 继承
 
- 属性
 
示例
以下示例使用计数器来确保对 方法的递归调用 Execute 数不超过MAX_RECURSIVE_CALLS常量定义的最大值。
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
let MAX_RECURSIVE_CALLS = 1000
let mutable ctr = 0
   
let rec execute () =
    ctr <- ctr + 1
    if ctr % 50 = 0 then
        printfn $"Call number {ctr} to the Execute method"
        
    if ctr <= MAX_RECURSIVE_CALLS then
        execute ()
        
    ctr <- ctr - 1
    
execute ()
printfn $"\nThe call counter: {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 使用 HRESULT COR_E_STACKOVERFLOW,其值0x800703E9。 中间 Localloc 语言 (IL) 指令引发 StackOverflowException。 有关对象的初始属性值 StackOverflowException 的列表,请参阅 StackOverflowException 构造函数。
从 .NET Framework 2.0 开始,无法使用块捕获StackOverflowException对象try/catch,并且默认终止相应的进程。 因此,应编写代码来检测并防止堆栈溢出。 例如,如果应用依赖于递归,请使用计数器或状态条件来终止递归循环。
有关此方法的插图,请参阅 示例 部分。
注意
将 HandleProcessCorruptedStateExceptionsAttribute 特性应用于引发 StackOverflowException 的方法不起作用。 仍无法处理用户代码中的异常。
如果应用 (CLR) 托管公共语言运行时,它可以指定 CLR 应卸载发生堆栈溢出异常的应用程序域,并允许相应的进程继续。 有关详细信息,请参阅 ICLRPolicyManager 接口。
构造函数
| StackOverflowException() | 
		 初始化 类的新实例 StackOverflowException ,将新实例的 属性设置为 Message 描述错误的系统提供的消息,例如“请求的操作导致堆栈溢出”。此消息考虑了当前系统区域性。  | 
        	
| StackOverflowException(String) | 
		 用指定的错误消息初始化 StackOverflowException 类的新实例。  | 
        	
| StackOverflowException(String, Exception) | 
		 使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 StackOverflowException 类的新实例。  | 
        	
属性
| Data | 
		 获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。 (继承自 Exception) | 
        	
| HelpLink | 
		 获取或设置指向与此异常关联的帮助文件链接。 (继承自 Exception) | 
        	
| HResult | 
		 获取或设置 HRESULT(一个分配给特定异常的编码数字值)。 (继承自 Exception) | 
        	
| InnerException | 
		 获取导致当前异常的 Exception 实例。 (继承自 Exception) | 
        	
| Message | 
		 获取描述当前异常的消息。 (继承自 Exception) | 
        	
| Source | 
		 获取或设置导致错误的应用程序或对象的名称。 (继承自 Exception) | 
        	
| StackTrace | 
		 获取调用堆栈上的即时框架字符串表示形式。 (继承自 Exception) | 
        	
| TargetSite | 
		 获取引发当前异常的方法。 (继承自 Exception) | 
        	
方法
| Equals(Object) | 
		 确定指定对象是否等于当前对象。 (继承自 Object) | 
        	
| GetBaseException() | 
		 当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。 (继承自 Exception) | 
        	
| GetHashCode() | 
		 作为默认哈希函数。 (继承自 Object) | 
        	
| GetObjectData(SerializationInfo, StreamingContext) | 
			 
		已过时.
	 
当在派生类中重写时,用关于异常的信息设置 SerializationInfo。 (继承自 Exception) | 
        	
| GetType() | 
		 获取当前实例的运行时类型。 (继承自 Exception) | 
        	
| MemberwiseClone() | 
		 创建当前 Object 的浅表副本。 (继承自 Object) | 
        	
| ToString() | 
		 创建并返回当前异常的字符串表示形式。 (继承自 Exception) | 
        	
事件
| SerializeObjectState | 
			 
		已过时.
	 
当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。 (继承自 Exception) |