Exception.InnerException Exception.InnerException Exception.InnerException Exception.InnerException Property

定義

取得造成目前例外狀況的 Exception 執行個體。 Gets the Exception instance that caused the current exception.

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

物件,描述造成目前例外狀況的錯誤。 An object that describes the error that caused the current exception. InnerException 屬性會傳回與傳遞到 Exception(String, Exception) 建構函式中相同的值;如果沒有提供內部例外狀況值給建構函式,則傳回 null The InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor. 這個屬性是唯讀的。 This property is read-only.

實作

範例

下列範例示範擲回和攔截例外狀況參考內部例外狀況。The following example demonstrates throwing and catching an exception that references an inner exception.

using namespace System;

public ref class AppException: public Exception
{
public:
   AppException(String^ message ) : Exception(message)
   {}

   AppException(String^ message, Exception^ inner) : Exception(message, inner)
   {}
};

public ref class Example
{
public:
   void ThrowInner()
   {
      throw gcnew AppException("Exception in ThrowInner method.");
   }

   void CatchInner()
   {
      try {
         this->ThrowInner();
      }
      catch (AppException^ e) {
         throw gcnew AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
      }
   }
};

int main()
{
   Example^ ex = gcnew Example();
   try {
      ex->CatchInner();
   }
   catch (AppException^ e) {
      Console::WriteLine("In catch block of Main method.");
      Console::WriteLine("Caught: {0}", e->Message);
      if (e->InnerException != nullptr)
         Console::WriteLine("Inner exception: {0}", e->InnerException);
   }
}
// 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.CatchInner()
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.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.CatchInner()

備註

當例外狀況X做為先前的例外狀況直接結果擲回Y,則InnerException屬性X應該包含的參考YWhen an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.

您可以使用 InnerException 屬性來取得造成目前例外狀況的例外狀況集合。Use the InnerException property to obtain the set of exceptions that led to the current exception.

您可以建立新的例外狀況攔截較早的例外狀況。You can create a new exception that catches an earlier exception. 處理第二個例外狀況可以使用從先前的例外狀況的其他資訊以更適當地處理錯誤的程式碼。The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.

假設有讀取檔案,並將該檔案中的資料格式的函式。Suppose that there is a function that reads a file and formats the data from that file. 在此範例中,為程式碼嘗試讀取檔案,IOException就會擲回。In this example, as the code tries to read the file, an IOException is thrown. 此函式會攔截IOException則會擲回FileNotFoundExceptionThe function catches the IOException and throws a FileNotFoundException. IOException無法存入InnerException屬性FileNotFoundException,啟用程式碼,以攔截FileNotFoundException檢查初始錯誤的原因。The IOException could be saved in the InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine the cause of the initial error.

InnerException屬性,其中包含內部例外狀況的參考,會設定例外狀況物件初始化時。The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

適用於