StackOverflowException 클래스

정의

실행 스택이 스택 크기를 초과할 때 throw되는 예외입니다. 이 클래스는 상속할 수 없습니다.

public ref class StackOverflowException sealed : SystemException
[System.Serializable]
public sealed class StackOverflowException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StackOverflowException : SystemException
public sealed class StackOverflowException : SystemException
[<System.Serializable>]
type StackOverflowException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackOverflowException = class
    inherit SystemException
type StackOverflowException = class
    inherit SystemException
Public NotInheritable Class StackOverflowException
Inherits SystemException
상속
StackOverflowException
특성

예제

다음 예제에서는 카운터를 사용하여 메서드에 대한 재귀 호출 Execute 수가 MAX_RECURSIVE_CALLS 상수에 정의된 최대값을 초과하지 않도록 합니다.

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

설명

StackOverflowException 는 일반적으로 매우 깊거나 바인딩되지 않은 재귀의 경우 실행 스택 오버플로 오류에 대해 throw됩니다. 따라서 코드에 무한 루프 또는 무한 재귀가 없는지 확인합니다.

StackOverflowException 는 값이 0x800703E9 HRESULT COR_E_STACKOVERFLOW 사용합니다. Localloc IL(중간 언어) 명령이 throw됩니다StackOverflowException. 개체의 초기 속성 값 StackOverflowException 목록은 생성자를 참조 StackOverflowException 하세요.

블록이 있는 개체 try/catchStackOverflowException catch할 수 없으며 해당 프로세스는 기본적으로 종료됩니다. 따라서 스택 오버플로를 감지하고 방지하는 코드를 작성해야 합니다. 예를 들어 앱이 재귀에 의존하는 경우 카운터 또는 상태 조건을 사용하여 재귀 루프를 종료합니다. 이 기술에 대한 일러스트레이션은 예제 섹션을 참조 하세요 .

메모

특성을 throw StackOverflowException 하는 HandleProcessCorruptedStateExceptionsAttribute 메서드에 적용해도 아무런 효과가 없습니다. 사용자 코드에서 예외를 처리할 수 없습니다.

앱이 CLR(공용 언어 런타임)을 호스트하는 경우 CLR이 스택 오버플로 예외가 발생하는 애플리케이션 도메인을 언로드하고 해당 프로세스를 계속하도록 지정할 수 있습니다. 자세한 내용은 ICLRPolicyManager 인터페이스를 참조하세요.

생성자

Name Description
StackOverflowException()

클래스의 StackOverflowException 새 인스턴스를 초기화하고 새 인스턴스의 속성을 "요청된 작업으로 인해 스택 오버플로가 발생했습니다."와 같은 오류를 설명하는 시스템 제공 메시지로 설정합니다 Message . 이 메시지는 현재 시스템 문화권을 고려합니다.

StackOverflowException(String, Exception)

지정된 오류 메시지와 이 예외의 StackOverflowException 원인인 내부 예외에 대한 참조를 사용하여 클래스의 새 인스턴스를 초기화합니다.

StackOverflowException(String)

지정된 오류 메시지를 사용하여 클래스의 StackOverflowException 새 인스턴스를 초기화합니다.

속성

Name Description
Data

예외에 대한 추가 사용자 정의 정보를 제공하는 키/값 쌍의 컬렉션을 가져옵니다.

(다음에서 상속됨 Exception)
HelpLink

이 예외와 연결된 도움말 파일에 대한 링크를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
HResult

특정 예외에 할당된 코딩된 숫자 값인 HRESULT를 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
InnerException

현재 예외를 Exception 발생시킨 인스턴스를 가져옵니다.

(다음에서 상속됨 Exception)
Message

현재 예외를 설명하는 메시지를 가져옵니다.

(다음에서 상속됨 Exception)
Source

오류를 발생시키는 애플리케이션 또는 개체의 이름을 가져오거나 설정합니다.

(다음에서 상속됨 Exception)
StackTrace

호출 스택에서 직접 실행 프레임의 문자열 표현을 가져옵니다.

(다음에서 상속됨 Exception)
TargetSite

현재 예외를 throw하는 메서드를 가져옵니다.

(다음에서 상속됨 Exception)

메서드

Name Description
Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetBaseException()

파생 클래스에서 재정의되는 경우 하나 이상의 후속 예외의 근본 원인인 값을 반환 Exception 합니다.

(다음에서 상속됨 Exception)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetObjectData(SerializationInfo, StreamingContext)

파생 클래스에서 재정의되는 경우 예외에 SerializationInfo 대한 정보를 사용하여 설정합니다.

(다음에서 상속됨 Exception)
GetType()

현재 인스턴스의 런타임 형식을 가져옵니다.

(다음에서 상속됨 Exception)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 예외의 문자열 표현을 만들고 반환합니다.

(다음에서 상속됨 Exception)

이벤트

Name Description
SerializeObjectState

예외에 대한 직렬화된 데이터를 포함하는 예외 상태 개체를 만들기 위해 예외가 serialize될 때 발생합니다.

(다음에서 상속됨 Exception)

적용 대상

추가 정보