StackOverflowException Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Undantaget som utlöses när körningsstacken överskrider stackstorleken. Det går inte att ärva den här klassen.
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
- Arv
- Attribut
Exempel
I följande exempel används en räknare för att säkerställa att antalet rekursiva anrop till Execute metoden inte överskrider ett maximum som definieras av MAX_RECURSIVE_CALLS konstant.
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
Kommentarer
StackOverflowException genereras för körningsstackens spillfel, vanligtvis i händelse av en mycket djup eller obundna rekursion. Se därför till att koden inte har en oändlig loop eller oändlig rekursion.
StackOverflowException använder HRESULT-COR_E_STACKOVERFLOW, som har värdet 0x800703E9. Den Localloc mellanliggande språkinstruktionen (IL) kastar StackOverflowException. En lista över initiala egenskapsvärden för ett StackOverflowException objekt finns i StackOverflowException konstruktorerna.
Du kan inte fånga ett StackOverflowException objekt med ett try/catch block och motsvarande process avslutas som standard. Därför bör du skriva koden för att identifiera och förhindra ett stackspill. Om din app till exempel är beroende av rekursion använder du en räknare eller ett tillståndsvillkor för att avsluta den rekursiva loopen. En bild av den här tekniken finns i avsnittet Exempel .
Note
Att tillämpa attributet på HandleProcessCorruptedStateExceptionsAttribute en metod som genererar en StackOverflowException har ingen effekt. Du kan fortfarande inte hantera undantaget från användarkoden.
Om din app är värd för CLR (Common Language Runtime) kan den ange att CLR ska ta bort programdomänen där stackens spillundantag sker och låta motsvarande process fortsätta. Mer information finns i ICLRPolicyManager-gränssnittet.
Konstruktorer
| Name | Description |
|---|---|
| StackOverflowException() |
Initierar en ny instans av StackOverflowException klassen och anger egenskapen för den nya instansen Message till ett meddelande från systemet som beskriver felet, till exempel "Den begärda åtgärden orsakade ett stackspill". Det här meddelandet tar hänsyn till den aktuella systemkulturen. |
| StackOverflowException(String, Exception) |
Initierar en ny instans av StackOverflowException klassen med ett angivet felmeddelande och en referens till det inre undantaget som är orsaken till det här undantaget. |
| StackOverflowException(String) |
Initierar en ny instans av StackOverflowException klassen med ett angivet felmeddelande. |
Egenskaper
| Name | Description |
|---|---|
| Data |
Hämtar en samling nyckel/värde-par som ger ytterligare användardefinierad information om undantaget. (Ärvd från Exception) |
| HelpLink |
Hämtar eller anger en länk till hjälpfilen som är associerad med det här undantaget. (Ärvd från Exception) |
| HResult |
Hämtar eller anger HRESULT, ett kodat numeriskt värde som har tilldelats ett specifikt undantag. (Ärvd från Exception) |
| InnerException |
Hämtar den Exception instans som orsakade det aktuella undantaget. (Ärvd från Exception) |
| Message |
Hämtar ett meddelande som beskriver det aktuella undantaget. (Ärvd från Exception) |
| Source |
Hämtar eller anger namnet på programmet eller objektet som orsakar felet. (Ärvd från Exception) |
| StackTrace |
Hämtar en strängrepresentation av de omedelbara ramarna i anropsstacken. (Ärvd från Exception) |
| TargetSite |
Hämtar den metod som utlöser det aktuella undantaget. (Ärvd från Exception) |
Metoder
| Name | Description |
|---|---|
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetBaseException() |
När den åsidosätts i en härledd klass returnerar den Exception som är rotorsaken till ett eller flera efterföljande undantag. (Ärvd från Exception) |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Föråldrad.
När åsidosättas i en härledd klass anger du SerializationInfo med information om undantaget. (Ärvd från Exception) |
| GetType() |
Hämtar körningstypen för den aktuella instansen. (Ärvd från Exception) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| ToString() |
Skapar och returnerar en strängrepresentation av det aktuella undantaget. (Ärvd från Exception) |
Händelser
| Name | Description |
|---|---|
| SerializeObjectState |
Föråldrad.
Inträffar när ett undantag serialiseras för att skapa ett undantagstillståndsobjekt som innehåller serialiserade data om undantaget. (Ärvd från Exception) |