Lire en anglais

Partager via


Exception.InnerException Propriété

Définition

Obtient l'instance Exception qui a provoqué l'exception actuelle.

C#
public Exception InnerException { get; }
C#
public Exception? InnerException { get; }

Valeur de propriété

Exception

Objet décrivant l'erreur qui a provoqué l'exception actuelle. La propriété InnerException retourne la même valeur que celle transmise au constructeur Exception(String, Exception), ou null si la valeur de l'exception interne n'a pas été fournie au constructeur. Cette propriété est en lecture seule.

Implémente

Exemples

L’exemple suivant illustre la levée et l’interception d’une exception qui fait référence à une exception interne.

C#
using System;

public class AppException : Exception
{
   public AppException(String message) : base (message)
   {}

   public AppException(String message, Exception inner) : base(message,inner) {}
}

public class Example
{
   public static void Main()
   {
      Example ex = new Example();

      try {
         ex.CatchInner();
      }
      catch(AppException e) {
         Console.WriteLine ("In catch block of Main method.");
         Console.WriteLine("Caught: {0}", e.Message);
         if (e.InnerException != null)
            Console.WriteLine("Inner exception: {0}", e.InnerException);
      }
   }

   public void ThrowInner ()
   {
      throw new AppException("Exception in ThrowInner method.");
   }

   public void CatchInner()
   {
      try {
         this.ThrowInner();
      }
      catch (AppException e) {
         throw new AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
      }
   }
}
// The example displays the following output:
//    In catch block of Main method.
//    Caught: Error in CatchInner caused by calling the ThrowInner method.
//    Inner exception: AppException: Exception in ThrowInner method.
//       at Example.ThrowInner()
//       at Example.CatchInner()

Remarques

Lorsqu’une exception X est levée en tant que résultat direct d’une exception Yprécédente, la InnerException propriété de X doit contenir une référence à Y.

Utilisez la propriété InnerException pour obtenir l'ensemble d'exceptions ayant provoqué l'exception en cours.

Vous pouvez créer une exception qui intercepte une exception antérieure. Le code qui gère la deuxième exception peut utiliser les informations supplémentaires de l’exception précédente pour gérer l’erreur de manière plus appropriée.

Supposons qu’il existe une fonction qui lit un fichier et met en forme les données à partir de ce fichier. Dans cet exemple, lorsque le code tente de lire le fichier, une IOException exception est levée. La fonction intercepte et IOException lève un FileNotFoundException. Il IOException peut être enregistré dans la InnerException propriété de l’objet FileNotFoundException, ce qui permet au code qui intercepte l’examen FileNotFoundException de la cause de l’erreur initiale.

La InnerException propriété, qui contient une référence à l’exception interne, est définie lors de l’initialisation de l’objet d’exception.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0