StackOverflowException Klasa

Definicja

Wyjątek zgłaszany, gdy stos wykonywania przekracza rozmiar stosu. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
StackOverflowException
Atrybuty

Przykłady

W poniższym przykładzie użyto licznika, aby upewnić się, że liczba wywołań Execute cyklicznych do metody nie przekracza maksymalnej wartości zdefiniowanej przez stałą 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

Uwagi

StackOverflowException jest zgłaszany w przypadku błędów przepełnienia stosu wykonywania, zwykle w przypadku bardzo głębokiej lub niepowiązanej rekursji. Upewnij się więc, że kod nie ma nieskończonej pętli ani nieskończonej rekursji.

StackOverflowException używa COR_E_STACKOVERFLOW HRESULT, która ma wartość 0x800703E9. Instrukcja języka pośredniego Localloc (IL) zgłasza błąd StackOverflowException. Aby uzyskać listę początkowych wartości właściwości obiektu StackOverflowException , zobacz StackOverflowException konstruktory.

Począwszy od .NET Framework 2.0, nie można przechwycić StackOverflowException obiektu z blokiemtry/catch, a odpowiedni proces jest domyślnie zakończony. W związku z tym należy napisać kod w celu wykrywania i zapobiegania przepełnieniu stosu. Jeśli na przykład aplikacja zależy od rekursji, użyj licznika lub warunku stanu, aby zakończyć pętlę rekursywną. Zobacz sekcję Przykłady , aby zapoznać się z ilustracją tej techniki.

Uwaga

Zastosowanie atrybutu HandleProcessCorruptedStateExceptionsAttribute do metody, która zgłasza StackOverflowException błąd, nie ma wpływu. Nadal nie można obsłużyć wyjątku z kodu użytkownika.

Jeśli aplikacja hostuje środowisko uruchomieniowe języka wspólnego (CLR), może określić, że środowisko CLR powinno zwolnić domenę aplikacji, w której wystąpi wyjątek przepełnienia stosu, i pozwolić na kontynuowanie odpowiedniego procesu. Aby uzyskać więcej informacji, zobacz ICLRPolicyManager Interface (Interfejs ICLRPolicyManager).

Konstruktory

StackOverflowException()

Inicjuje nowe wystąpienie StackOverflowException klasy, ustawiając Message właściwość nowego wystąpienia na komunikat dostarczony przez system, który opisuje błąd, taki jak "Żądana operacja spowodowała przepełnienie stosu". Ten komunikat uwzględnia bieżącą kulturę systemu.

StackOverflowException(String)

Inicjuje StackOverflowException nowe wystąpienie klasy z określonym komunikatem o błędzie.

StackOverflowException(String, Exception)

Inicjuje nowe wystąpienie StackOverflowException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

Właściwości

Data

Pobiera kolekcję par klucz/wartość, które zapewniają dodatkowe informacje zdefiniowane przez użytkownika dotyczące wyjątku.

(Odziedziczone po Exception)
HelpLink

Pobiera lub ustawia link do pliku pomocy skojarzonego z tym wyjątkiem.

(Odziedziczone po Exception)
HResult

Pobiera lub ustawia HRESULT, zakodowaną wartość liczbową przypisaną do określonego wyjątku.

(Odziedziczone po Exception)
InnerException

Exception Pobiera wystąpienie, które spowodowało bieżący wyjątek.

(Odziedziczone po Exception)
Message

Pobiera komunikat opisujący bieżący wyjątek.

(Odziedziczone po Exception)
Source

Pobiera lub ustawia nazwę aplikacji lub obiektu, który powoduje błąd.

(Odziedziczone po Exception)
StackTrace

Pobiera reprezentację ciągu natychmiastowych ramek na stosie wywołań.

(Odziedziczone po Exception)
TargetSite

Pobiera metodę, która zgłasza bieżący wyjątek.

(Odziedziczone po Exception)

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetBaseException()

Po przesłonięciu w klasie pochodnej funkcja zwraca Exception główną przyczynę co najmniej jednego kolejnego wyjątku.

(Odziedziczone po Exception)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetObjectData(SerializationInfo, StreamingContext)
Przestarzałe.

Po zastąpieniu w klasie pochodnej ustawia SerializationInfo element z informacjami o wyjątku.

(Odziedziczone po Exception)
GetType()

Pobiera typ środowiska uruchomieniowego bieżącego wystąpienia.

(Odziedziczone po Exception)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Tworzy i zwraca reprezentację ciągu bieżącego wyjątku.

(Odziedziczone po Exception)

Zdarzenia

SerializeObjectState
Przestarzałe.

Występuje, gdy wyjątek jest serializowany w celu utworzenia obiektu stanu wyjątku, który zawiera serializowane dane dotyczące wyjątku.

(Odziedziczone po Exception)

Dotyczy

Zobacz też