Exception.InnerException Proprietà

Definizione

Ottiene l'istanza di Exception che ha causato l'eccezione corrente.

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

Valore della proprietà

Exception

Oggetto che descrive l'errore che ha causato l'eccezione corrente. La proprietà InnerException restituisce lo stesso valore passato al costruttore Exception(String, Exception), oppure null se il valore dell'eccezione interna non è stato fornito al costruttore. Questa proprietà è di sola lettura.

Implementazioni

Esempio

Nell'esempio seguente viene illustrato come generare e rilevare un'eccezione che fa riferimento a un'eccezione interna.

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()

Commenti

Quando viene generata un'eccezione come risultato diretto di un'eccezione precedente, la InnerException proprietà di X deve contenere un riferimento a Y.X Y

Utilizzare la proprietà InnerException per ottenere il set di eccezioni che ha causato l'eccezione corrente.

È possibile creare una nuova eccezione che rileva un'eccezione precedente. Il codice che gestisce la seconda eccezione può usare le informazioni aggiuntive dell'eccezione precedente per gestire l'errore in modo più appropriato.

Si supponga che sia presente una funzione che legge un file e formatta i dati da tale file. In questo esempio, quando il codice tenta di leggere il file, viene generato un IOException oggetto . La funzione rileva e IOException genera un FileNotFoundExceptionoggetto . L'oggetto IOException può essere salvato nella InnerException proprietà di FileNotFoundException, abilitando il codice che rileva FileNotFoundException l'oggetto per esaminare la causa dell'errore iniziale.

La InnerException proprietà, che contiene un riferimento all'eccezione interna, viene impostata all'inizializzazione dell'oggetto eccezione.

Si applica a

Prodotto Versioni
.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