次の方法で共有


StackOverflowException クラス

保留状態のメソッド呼び出しが多くなりすぎ、実行スタックがオーバーフローした場合にスローされる例外。このクラスは継承できません。

この型のすべてのメンバの一覧については、StackOverflowException メンバ を参照してください。

System.Object
   System.Exception
      System.SystemException
         System.StackOverflowException

<Serializable>
NotInheritable Public Class StackOverflowException   Inherits SystemException
[C#]
[Serializable]
public sealed class StackOverflowException : SystemException
[C++]
[Serializable]
public __gc __sealed class StackOverflowException : public   SystemException
[JScript]
public
   Serializable
class StackOverflowException extends SystemException

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

StackOverflowException は、再帰の回数がかなり多い場合や無限に続く場合などに生じる、実行スタックのオーバーフローによるエラーに対してスローされます。

MSIL (Microsoft Intermediate Language) の Localloc 命令は、 StackOverflowException をスローします。

StackOverflowException は、値 0x800703E9 を保持する HRESULT COR_E_STACKOVERFLOW を使用します。

StackOverflowException のインスタンスの初期プロパティ値の一覧については、 StackOverflowException コンストラクタのトピックを参照してください。

使用例

 

Imports System
Imports System.IO

Namespace StackOverflowSpace

    '/ This class contains the entry point for a very simple program
    '/ that will illustrate the StackOverflowException.  The exception
    '/ will be thrown from inside the function call but will keep
    '/ traversing up the stack until we catch it inside Main.
    Class StackOverflowExample

        'entry point for the sample
        Shared Sub Main()
            'attempt to call an infinitely recursive function
            Try
                Recurse.InfiniteRecurse()

                'this will fire when the recursion went too deep
            Catch overFlowExcept As System.StackOverflowException
                Console.WriteLine(overFlowExcept.Message)
                Console.WriteLine("Program will now end.")
                Return
            End Try

            Console.WriteLine("This line will never execute")
        End Sub 'Main 

    End Class 'StackOverflowExample


    '/ Recurse contains one static member designed to recurse forever.
    '/ We could try to catch it inside this function and deal with it 
    '/ here, but it's hard to do much without any stack left.
    Class Recurse

        'keeps calling itself forever until it
        'overflows the stack and throws a exception
        Public Shared Sub InfiniteRecurse()
            InfiniteRecurse()
            Return
        End Sub 'InfiniteRecurse

    End Class 'End of Class Recurse 

End Namespace 'End of StackOverflowSpace


[C#] 

using System;
using System.IO;

namespace StackOverflowSpace
{
    /// <summary>
    /// This class contains the entry point for a very simple program
    /// that will illustrate the StackOverflowException.  The exception
    /// will be thrown from inside the function call but will keep
    /// traversing up the stack until we catch it inside Main.
    /// </summary>
    class StackOverflowExample    
    {

        //entry point for the sample
        static void Main() 
        {
            //attempt to call an infinitely recursive function
            try {
                Recurse.InfiniteRecurse();
            }
            
            //this will fire when the recursion went too deep
            catch (System.StackOverflowException overFlowExcept) {
                Console.WriteLine(overFlowExcept.Message);
                Console.WriteLine("Program will now end.");
                return;
            }
            
            Console.WriteLine("This line will never execute");
            
        }    //end of main                             
    }    //end of class Stackoverflow

    /// <summary>
    /// Recurse contains one static member designed to recurse forever.
    /// We could try to catch it inside this function and deal with it 
    /// here, but it's hard to do much without any stack left.
    /// </summary>
    class Recurse {

        //keeps calling itself forever until it
        //overflows the stack and throws a exception
        static public void InfiniteRecurse() {
            InfiniteRecurse();        
            return;
        }    

    }    //end of class Recurse
}    //end of StackOverFlowSpace Namespace


[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

/// <summary>
/// Recurse contains one static member designed to recurse forever.
/// We could try to catch it inside this function and deal with it
/// here, but it's hard to do much without any stack left.
/// </summary>
class Recurse {
public:
   //keeps calling itself forever until it
   //overflows the stack and throws a exception
   static 
      void InfiniteRecurse() {
         InfiniteRecurse();
         return;
      };
};    //end of class Recurse

/// <summary>
/// This function contains the entry point for a very simple program
/// that will illustrate the StackOverflowException.  The exception
/// will be thrown from inside the function call but will keep
/// traversing up the stack until we catch it inside main.
/// </summary>

//entry point for the sample
int main() {
   //attempt to call an infinitely recursive function
   try {
      Recurse::InfiniteRecurse();
   }

   //this will fire when the recursion went too deep
   catch (System::StackOverflowException* overFlowExcept) {
      Console::WriteLine(overFlowExcept->Message);
      Console::WriteLine(S"Program will now end.");
      return 0;
   }

   Console::WriteLine(S"This line will never execute");

}    //end of main

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

StackOverflowException メンバ | System 名前空間 | Exception | 例外の処理とスロー