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 、のプロパティにはYへの参照が含まれている必要があります。When 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. 2つ目の例外を処理するコードは、エラーをより適切に処理するために、前の例外の追加情報を使用できます。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し、をFileNotFoundExceptionスローします。The function catches the IOException and throws a FileNotFoundException. をのInnerException FileNotFoundExceptionプロパティにFileNotFoundException保存して、をキャッチしたコードで最初のエラーの原因を調べることができます。IOExceptionThe 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.

内部例外への参照を保持するプロパティは、例外オブジェクトの初期化時に設定されます。InnerExceptionThe InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

適用対象