Exception.InnerException 속성

정의

현재 예외를 발생시킨 Exception 인스턴스를 가져옵니다.

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

속성 값

Exception

현재 예외를 발생시키는 오류를 설명하는 개체입니다. InnerException 속성은 Exception(String, Exception) 생성자로 전달된 것과 동일한 값을 반환하거나 생성자에 내부 예외 값을 제공하지 않은 경우 null을 반환합니다. 이 속성은 읽기 전용입니다.

구현

예제

다음 예제에서는 내부 예외를 참조하는 예외를 throw하고 catch하는 방법을 보여 줍니다.

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.ThrowInner()
//       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.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()

설명

예외가 이전 예외 X Y의 직접적인 결과로 throw되면 속성 XInnerException 대한 참조Y가 포함되어야 합니다.

속성을 InnerException 사용하여 현재 예외로 이어진 예외 집합을 가져옵니다.

이전 예외를 catch하는 새 예외를 만들 수 있습니다. 두 번째 예외를 처리하는 코드는 이전 예외의 추가 정보를 사용하여 오류를 보다 적절하게 처리할 수 있습니다.

파일을 읽고 해당 파일의 데이터 형식을 지정하는 함수가 있다고 가정합니다. 이 예제에서는 코드가 파일을 읽으려고 할 때 throw IOException 됩니다. 함수는 해당 함수를 IOException catch하고 throw FileNotFoundException합니다. 초기 IOException 오류의 FileNotFoundException원인을 검사하기 위해 catch FileNotFoundException 하는 코드를 사용하도록 설정하여 속성에 저장할 InnerException 수 있습니다.

내부 예외에 대한 참조를 보유하는 속성은 InnerException 예외 개체를 초기화할 때 설정됩니다.

적용 대상