Uredi

Deli z drugimi prek


StackOverflowException Class

Definition

The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited.

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
Inheritance
StackOverflowException
Attributes

Examples

The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant.

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

Remarks

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion.

StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9. The Localloc intermediate language (IL) instruction throws StackOverflowException. For a list of initial property values for a StackOverflowException object, see the StackOverflowException constructors.

Starting with the .NET Framework 2.0, you can't catch a StackOverflowException object with a try/catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow. For example, if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. See the Examples section for an illustration of this technique.

Note

Applying the HandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws a StackOverflowException has no effect. You still cannot handle the exception from user code.

If your app hosts the common language runtime (CLR), it can specify that the CLR should unload the application domain where the stack overflow exception occurs and let the corresponding process continue. For more information, see ICLRPolicyManager Interface.

Constructors

StackOverflowException()

Initializes a new instance of the StackOverflowException class, setting the Message property of the new instance to a system-supplied message that describes the error, such as "The requested operation caused a stack overflow." This message takes into account the current system culture.

StackOverflowException(String, Exception)

Initializes a new instance of the StackOverflowException class with a specified error message and a reference to the inner exception that is the cause of this exception.

StackOverflowException(String)

Initializes a new instance of the StackOverflowException class with a specified error message.

Properties

Data

Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink

Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException

Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message

Gets a message that describes the current exception.

(Inherited from Exception)
Source

Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace

Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite

Gets the method that throws the current exception.

(Inherited from Exception)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException()

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

When overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
GetType()

Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Creates and returns a string representation of the current exception.

(Inherited from Exception)

Events

SerializeObjectState
Obsolete.

Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

Applies to

See also