Condividi tramite


Exception.InnerException Proprietà

Definizione

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

public:
 property Exception ^ InnerException { Exception ^ get(); };
public Exception InnerException { get; }
public Exception? InnerException { get; }
member this.InnerException : Exception
Public ReadOnly Property InnerException As Exception

Valore della proprietà

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

Implementazioni

Esempio

Nell'esempio seguente viene illustrata la generazione e l'intercettazione di un'eccezione che fa riferimento a un'eccezione interna.

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

type AppException =
    inherit Exception
    
    new (message: string) = { inherit Exception(message) }

    new (message: string, inner) = { inherit Exception(message, inner) }

let throwInner () =
    raise (AppException "Exception in throwInner function.")
    ()

let catchInner () =
    try
        throwInner ()
    with :? AppException as e ->
        raise (AppException("Error in catchInner caused by calling the throwInner function.", e) )

[<EntryPoint>]
let main _ =
    try
        catchInner ()
    with :? AppException as e ->
        printfn "In with block of main function."
        printfn $"Caught: {e.Message}"
        if e.InnerException <> null then
            printfn $"Inner exception: {e.InnerException}"
    0
// The example displays the following output:
//    In with block of main function.
//    Caught: Error in catchInner caused by calling the throwInner function.
//    Inner exception: Example+AppException: Exception in throwInner function.
//       at Example.throwInner()
//       at Example.catchInner()
Public Class AppException : Inherits Exception
   Public Sub New(message As String)
      MyBase.New(message)
   End Sub
   
   Public Sub New(message As String, inner As Exception)
      MyBase.New(message, inner)
   End Sub
End Class

Public Class Example
   Public Shared Sub Main()
      Dim testInstance As New Example()
      Try
         testInstance.CatchInner()
      Catch e As AppException
         Console.WriteLine ("In catch block of Main method.")
         Console.WriteLine("Caught: {0}", e.Message)
         If e.InnerException IsNot Nothing Then
            Console.WriteLine("Inner exception: {0}", e.InnerException)
         End If
      End Try
   End Sub
   
   Public Sub ThrowInner()
      Throw New AppException("Exception in ThrowInner method.")
   End Sub
   
   Public Sub CatchInner()
      Try
         Me.ThrowInner()
      Catch e As AppException
         Throw New AppException("Error in CatchInner caused by calling the ThrowInner method.", e)
      End Try
   End Sub
End Class
' 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 X come risultato diretto di un'eccezione Yprecedente, la InnerException proprietà di X deve contenere un riferimento a Y.

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

È possibile creare una nuova eccezione che intercetta 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 generata un'eccezione IOException . La funzione intercetta e IOException genera un'eccezione FileNotFoundException. Può IOException essere salvato nella InnerException proprietà di FileNotFoundException, abilitando il codice che intercetta per FileNotFoundException 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