Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

StackOverflowException Class

Definition

Namespace:
System
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
StackOverflowException.cs
Source:
StackOverflowException.cs
Source:
StackOverflowException.cs
Source:
StackOverflowException.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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

Examples

The following example uses a counter to ensure that the number of recursive calls to theExecute 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 = 1000let 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 SubEnd 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. TheLocalloc intermediate language (IL) instruction throwsStackOverflowException. For a list of initial property values for aStackOverflowException object, see theStackOverflowException constructors.

You can't catch aStackOverflowException object with atry/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. For an illustration of this technique, see theExamples section.

Note

Applying theHandleProcessCorruptedStateExceptionsAttribute attribute to a method that throws aStackOverflowException 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, seeICLRPolicyManager Interface.

Constructors

StackOverflowException()

Initializes a new instance of theStackOverflowException class, setting theMessage 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 theStackOverflowException 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 theStackOverflowException 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 fromException)
HelpLink

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

(Inherited fromException)
HResult

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

(Inherited fromException)
InnerException

Gets theException instance that caused the current exception.

(Inherited fromException)
Message

Gets a message that describes the current exception.

(Inherited fromException)
Source

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

(Inherited fromException)
StackTrace

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

(Inherited fromException)
TargetSite

Gets the method that throws the current exception.

(Inherited fromException)

Methods

Equals(Object)

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

(Inherited fromObject)
GetBaseException()

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

(Inherited fromException)
GetHashCode()

Serves as the default hash function.

(Inherited fromObject)
GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

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

(Inherited fromException)
GetType()

Gets the runtime type of the current instance.

(Inherited fromException)
MemberwiseClone()

Creates a shallow copy of the currentObject.

(Inherited fromObject)
ToString()

Creates and returns a string representation of the current exception.

(Inherited fromException)

Events

SerializeObjectState
Obsolete.

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

(Inherited fromException)

Applies to

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo