System Namespace
The exception that is thrown when the execution stack overflows by having too many pending method calls. This class cannot be inherited.
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.
[ VB ]
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
Response.WriteLine ( overFlowExcept.Message )
Response.WriteLine ( "Program will now end." )
Return
End Try
Response.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 ) {
Response.WriteLine ( overFlowExcept.Message );
Response.WriteLine ( "Program will now end." );
return;
}
Response.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
Exception