StackOverflowException Klasse
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Die Ausnahme, die ausgelöst wird, wenn der Ausführungsstapel die Stapelgröße überschreitet. Diese Klasse kann nicht vererbt werden.
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
- Vererbung
- Attribute
Beispiele
Im folgenden Beispiel wird ein Zähler verwendet, um sicherzustellen, dass die Anzahl rekursiver Aufrufe der Execute Methode nicht ein Maximum überschreitet, das durch die MAX_RECURSIVE_CALLS Konstante definiert ist.
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
Hinweise
StackOverflowException wird für Ausführungsstapelüberlauffehler ausgelöst, in der Regel bei einer sehr tiefen oder ungebundenen Rekursion. Stellen Sie daher sicher, dass Ihr Code keine endlose Schleife oder unendliche Rekursion hat.
StackOverflowException verwendet die HRESULT-COR_E_STACKOVERFLOW, die den Wert 0x800703E9 hat. Die Localloc Il-Anweisung (Intermediate Language) löst StackOverflowExceptionaus. Eine Liste der anfänglichen Eigenschaftswerte für ein StackOverflowException Objekt finden Sie in den StackOverflowException Konstruktoren.
Sie können ein StackOverflowException Objekt nicht mit einem try/catch Block abfangen, und der entsprechende Prozess wird standardmäßig beendet. Daher sollten Sie Ihren Code schreiben, um einen Stapelüberlauf zu erkennen und zu verhindern. Wenn Ihre App beispielsweise von rekursion abhängt, verwenden Sie einen Zähler oder eine Zustandsbedingung, um die rekursive Schleife zu beenden. Eine Abbildung dieser Technik finden Sie im Abschnitt "Beispiele ".
Note
Das Anwenden des HandleProcessCorruptedStateExceptionsAttribute Attributs auf eine Methode, die ausgelöst StackOverflowException wird, hat keine Auswirkung. Die Ausnahme von Benutzercode kann weiterhin nicht behandelt werden.
Wenn Ihre App die Common Language Runtime (CLR) hostt, kann sie angeben, dass die CLR die Anwendungsdomäne entladen soll, in der die Stapelüberlaufausnahme auftritt, und den entsprechenden Prozess fortsetzen zu lassen. Weitere Informationen finden Sie unter ICLRPolicyManager Interface.
Konstruktoren
| Name | Beschreibung |
|---|---|
| StackOverflowException() |
Initialisiert eine neue Instanz der StackOverflowException Klasse, wobei die Message Eigenschaft der neuen Instanz auf eine vom System bereitgestellte Meldung festgelegt wird, die den Fehler beschreibt, z. B. "Der angeforderte Vorgang hat einen Stapelüberlauf verursacht". Diese Meldung berücksichtigt die aktuelle Systemkultur. |
| StackOverflowException(String, Exception) |
Initialisiert eine neue Instanz der StackOverflowException Klasse mit einer angegebenen Fehlermeldung und einem Verweis auf die innere Ausnahme, die die Ursache dieser Ausnahme ist. |
| StackOverflowException(String) |
Initialisiert eine neue Instanz der StackOverflowException Klasse mit einer angegebenen Fehlermeldung. |
Eigenschaften
| Name | Beschreibung |
|---|---|
| Data |
Ruft eine Auflistung von Schlüssel-Wert-Paaren ab, die zusätzliche benutzerdefinierte Informationen zur Ausnahme bereitstellen. (Geerbt von Exception) |
| HelpLink |
Dient zum Abrufen oder Festlegen eines Links zur Hilfedatei, die dieser Ausnahme zugeordnet ist. (Geerbt von Exception) |
| HResult |
Dient zum Abrufen oder Festlegen von HRESULT, einem codierten numerischen Wert, der einer bestimmten Ausnahme zugewiesen ist. (Geerbt von Exception) |
| InnerException |
Ruft die Exception Instanz ab, die die aktuelle Ausnahme verursacht hat. (Geerbt von Exception) |
| Message |
Ruft eine Nachricht ab, die die aktuelle Ausnahme beschreibt. (Geerbt von Exception) |
| Source |
Dient zum Abrufen oder Festlegen des Namens der Anwendung oder des Objekts, das den Fehler verursacht. (Geerbt von Exception) |
| StackTrace |
Ruft eine Zeichenfolgendarstellung der unmittelbaren Frames im Aufrufstapel ab. (Geerbt von Exception) |
| TargetSite |
Ruft die Methode ab, die die aktuelle Ausnahme auslöst. (Geerbt von Exception) |
Methoden
| Name | Beschreibung |
|---|---|
| Equals(Object) |
Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist. (Geerbt von Object) |
| GetBaseException() |
Wenn sie in einer abgeleiteten Klasse überschrieben wird, wird die Exception Ursache einer oder mehrerer nachfolgenden Ausnahmen zurückgegeben. (Geerbt von Exception) |
| GetHashCode() |
Dient als Standardhashfunktion. (Geerbt von Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Wenn sie in einer abgeleiteten Klasse überschrieben wird, werden die SerializationInfo Informationen zur Ausnahme festgelegt. (Geerbt von Exception) |
| GetType() |
Ruft den Laufzeittyp der aktuellen Instanz ab. (Geerbt von Exception) |
| MemberwiseClone() |
Erstellt eine flache Kopie der aktuellen Object. (Geerbt von Object) |
| ToString() |
Erstellt und gibt eine Zeichenfolgendarstellung der aktuellen Ausnahme zurück. (Geerbt von Exception) |
Ereignisse
| Name | Beschreibung |
|---|---|
| SerializeObjectState |
Tritt auf, wenn eine Ausnahme serialisiert wird, um ein Ausnahmestatusobjekt zu erstellen, das serialisierte Daten zu der Ausnahme enthält. (Geerbt von Exception) |