StackOverflowException Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Exception levée lorsque la pile d’exécution dépasse la taille de la pile. Cette classe ne peut pas être héritée.
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
- Héritage
- Attributs
Exemples
L’exemple suivant utilise un compteur pour s’assurer que le nombre d’appels récursifs à la Execute méthode ne dépasse pas un maximum défini par la constante 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
Remarques
StackOverflowException est levée pour les erreurs de dépassement de capacité de la pile d’exécution, généralement en cas de récursivité très profonde ou non liée. Vérifiez donc que votre code n’a pas de boucle infinie ou de récursivité infinie.
StackOverflowException utilise le COR_E_STACKOVERFLOW HRESULT, qui a la valeur 0x800703E9. L’instruction Localloc de langage intermédiaire (IL) lève StackOverflowException. Pour obtenir la liste des valeurs de propriété initiales d’un StackOverflowException objet, consultez les StackOverflowException constructeurs.
Vous ne pouvez pas intercepter un StackOverflowException objet avec un try/catch bloc et le processus correspondant est arrêté par défaut. Par conséquent, vous devez écrire votre code pour détecter et empêcher un dépassement de capacité de pile. Par exemple, si votre application dépend de la récursivité, utilisez un compteur ou une condition d’état pour arrêter la boucle récursive. Pour obtenir une illustration de cette technique, consultez la section Exemples .
Note
L’application de l’attribut HandleProcessCorruptedStateExceptionsAttribute à une méthode qui lève un StackOverflowException effet n’a aucun effet. Vous ne pouvez toujours pas gérer l’exception du code utilisateur.
Si votre application héberge le Common Language Runtime (CLR), elle peut spécifier que le CLR doit décharger le domaine d’application où l’exception de dépassement de pile se produit et laisser le processus correspondant continuer. Pour plus d’informations, consultez l’interface ICLRPolicyManager.
Constructeurs
| Nom | Description |
|---|---|
| StackOverflowException() |
Initialise une nouvelle instance de la StackOverflowException classe, en définissant la Message propriété de la nouvelle instance sur un message fourni par le système qui décrit l’erreur, par exemple « L’opération demandée a provoqué un dépassement de capacité de pile ». Ce message prend en compte la culture système actuelle. |
| StackOverflowException(String, Exception) |
Initialise une nouvelle instance de la StackOverflowException classe avec un message d’erreur spécifié et une référence à l’exception interne qui est la cause de cette exception. |
| StackOverflowException(String) |
Initialise une nouvelle instance de la StackOverflowException classe avec un message d’erreur spécifié. |
Propriétés
| Nom | Description |
|---|---|
| Data |
Obtient une collection de paires clé/valeur qui fournissent des informations supplémentaires définies par l’utilisateur sur l’exception. (Hérité de Exception) |
| HelpLink |
Obtient ou définit un lien vers le fichier d’aide associé à cette exception. (Hérité de Exception) |
| HResult |
Obtient ou définit HRESULT, valeur numérique codée affectée à une exception spécifique. (Hérité de Exception) |
| InnerException |
Obtient l’instance Exception qui a provoqué l’exception actuelle. (Hérité de Exception) |
| Message |
Obtient un message qui décrit l’exception actuelle. (Hérité de Exception) |
| Source |
Obtient ou définit le nom de l’application ou de l’objet qui provoque l’erreur. (Hérité de Exception) |
| StackTrace |
Obtient une représentation sous forme de chaîne des images immédiates sur la pile des appels. (Hérité de Exception) |
| TargetSite |
Obtient la méthode qui lève l’exception actuelle. (Hérité de Exception) |
Méthodes
| Nom | Description |
|---|---|
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetBaseException() |
En cas de substitution dans une classe dérivée, retourne la Exception qui est la cause racine d’une ou plusieurs exceptions ultérieures. (Hérité de Exception) |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
En cas de substitution dans une classe dérivée, définit l'SerializationInfo avec des informations sur l’exception. (Hérité de Exception) |
| GetType() |
Obtient le type d’exécution de l’instance actuelle. (Hérité de Exception) |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| ToString() |
Crée et retourne une représentation sous forme de chaîne de l’exception actuelle. (Hérité de Exception) |
Événements
| Nom | Description |
|---|---|
| SerializeObjectState |
Se produit lorsqu’une exception est sérialisée pour créer un objet d’état d’exception qui contient des données sérialisées sur l’exception. (Hérité de Exception) |