System.StackOverflowException Class
Represents the error that occurs when the execution stack overflows due to too many method calls.

See Also: StackOverflowException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StackOverflowException : SystemException

Remarks

Note:

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

The localloc CIL instruction throws StackOverflowException.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates an error that causes a StackOverflowException exception.

C# Example

using System;
public class StackOverflowExample {
   public static void recursion() { recursion(); }
   public static void Main() {
      try {
         recursion();
      }
      catch(StackOverflowException e) {
         Console.WriteLine("Error caught: {0}", e);
      }
   }
}
   

The output is

Error caught: System.StackOverflowException: Exception of type System.StackOverflowException was thrown.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0