Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

StackOverflowException Class

The exception that is thrown when the execution stack overflows by having too many pending method calls. This class cannot be inherited.

For a list of all members of this type, see StackOverflowException Members.

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

[Visual Basic]
<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

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.

The Localloc Microsoft intermediate language (MSIL) instruction throws StackOverflowException.

StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, that has the value 0x800703E9.

For a list of initial property values for an instance of StackOverflowException, see the StackOverflowException constructors.

Example

[Visual Basic] 

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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

StackOverflowException Members | System Namespace | Exception | Handling and Throwing Exceptions

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.