StackOverflowException クラス

定義

実行スタックがスタック サイズを超えるとスローされる例外。 このクラスは継承できません。

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
継承
StackOverflowException
属性

次の例では、カウンターを使用して、 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 では、0x800703E9値を持つ HRESULT COR_E_STACKOVERFLOWが使用されます。 Localloc中間言語 (IL) 命令は、StackOverflowExceptionをスローします。 StackOverflowException オブジェクトの初期プロパティ値の一覧については、StackOverflowExceptionコンストラクターを参照してください。

StackOverflowException try / ブロックを持つcatch オブジェクトをキャッチすることはできません。対応するプロセスは既定で終了します。 そのため、スタック オーバーフローを検出して防止するコードを記述する必要があります。 たとえば、アプリが再帰に依存している場合は、カウンターまたは状態条件を使用して再帰ループを終了します。 この手法の図については、「例」セクション 参照してください。

Note

HandleProcessCorruptedStateExceptionsAttributeをスローするメソッドにStackOverflowException属性を適用しても効果はありません。 ユーザー コードからの例外は引き続き処理できません。

アプリが共通言語ランタイム (CLR) をホストしている場合は、CLR がスタック オーバーフロー例外が発生するアプリケーション ドメインをアンロードし、対応するプロセスを続行することを指定できます。 詳細については、「 ICLRPolicyManager インターフェイス」を参照してください。

コンストラクター

名前 説明