Exception.InnerException 속성

정의

현재 예외를 발생시킨 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.

구현

예제

다음 예제에서는 throw 및 내부 예외를 참조 하는 예외를 catch 하는 방법을 보여 줍니다.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 이전 예외의 직접 결과로 throw 되는 YInnerException 속성을 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.

이전 예외를 catch 하는 새 예외를 만들 수 있습니다.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 throw 됩니다.In this example, as the code tries to read the file, an IOException is thrown. 함수를 catch 합니다 IOException throw를 FileNotFoundException입니다.The function catches the IOException and throws a FileNotFoundException. IOException 에 저장 될 수는 InnerException 의 속성을 FileNotFoundException를 catch 하는 코드를 사용 하도록 설정는 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.

적용 대상