Exception.GetBaseException 메서드

정의

파생 클래스에서 재정의된 경우 하나 이상의 후속 예외의 근본 원인이 되는 Exception 을 반환합니다.

public:
 virtual Exception ^ GetBaseException();
public virtual Exception GetBaseException ();
abstract member GetBaseException : unit -> Exception
override this.GetBaseException : unit -> Exception
Public Overridable Function GetBaseException () As Exception

반환

Exception

예외의 체인에서 throw된 첫째 예외입니다. 현재 예외의 InnerException 속성이 null 참조(Visual Basic에서는Nothing )인 경우, 이 속성은 현재 예외를 반환합니다.

구현

예제

다음 코드 예제에서는 두 개의 파생 클래스 Exception 를 정의합니다. 예외를 강제로 실행한 다음 각 파생 클래스와 함께 다시 throw합니다. 이 코드는 메서드를 GetBaseException 사용하여 원래 예외를 검색하는 방법을 보여줍니다.

// Example for the Exception::GetBaseException method.
using namespace System;

namespace NDP_UE_CPP
{

   // Define two derived exceptions to demonstrate nested exceptions.
   ref class SecondLevelException: public Exception
   {
   public:
      SecondLevelException( String^ message, Exception^ inner )
         : Exception( message, inner )
      {}

   };

   ref class ThirdLevelException: public Exception
   {
   public:
      ThirdLevelException( String^ message, Exception^ inner )
         : Exception( message, inner )
      {}

   };


   // DivideBy0 forces a division by 0 and throws a second exception.
   void DivideBy0()
   {
      try
      {
         int zero = 0;
         int ecks = 1 / zero;
      }
      catch ( Exception^ ex ) 
      {
         throw gcnew SecondLevelException( "Forced a division by 0 and threw "
         "a second exception.",ex );
      }

   }


   // This function catches the exception from the called function
   // DivideBy0( ) and throws another in response.
   void Rethrow()
   {
      try
      {
         DivideBy0();
      }
      catch ( Exception^ ex ) 
      {
         throw gcnew ThirdLevelException( "Caught the second exception and "
         "threw a third in response.",ex );
      }

   }

}

int main()
{
   Console::WriteLine( "This example of Exception.GetBaseException "
   "generates the following output." );
   Console::WriteLine( "\nThe program forces a division by 0, "
   "then throws the exception \ntwice more, "
   "using a different derived exception each time.\n" );
   try
   {
      
      // This function calls another that forces a division by 0.
      NDP_UE_CPP::Rethrow();
   }
   catch ( Exception^ e ) 
   {
      Exception^ current;
      Console::WriteLine( "Unwind the nested exceptions using "
      "the InnerException property:\n" );
      
      // This code unwinds the nested exceptions using the 
      // InnerException property.
      current = e;
      while ( current != (Object^)0 )
      {
         Console::WriteLine( current->ToString() );
         Console::WriteLine();
         current = current->InnerException;
      }
      
      // Display the innermost exception.
      Console::WriteLine( "Display the base exception using the \n"
      "GetBaseException method:\n" );
      Console::WriteLine( e->GetBaseException()->ToString() );
   }

}

/*
This example of Exception.GetBaseException generates the following output.

The program forces a division by 0, then throws the exception
twice more, using a different derived exception each time.

Unwind the nested exceptions using the InnerException property:

NDP_UE_CPP.ThirdLevelException: Caught the second exception and threw a third i
n response. ---> NDP_UE_CPP.SecondLevelException: Forced a division by 0 and th
rew a second exception. ---> System.DivideByZeroException: Attempted to divide
by zero.
   at NDP_UE_CPP.DivideBy0()
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.DivideBy0()
   at NDP_UE_CPP.Rethrow()
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.Rethrow()
   at main()

NDP_UE_CPP.SecondLevelException: Forced a division by 0 and threw a second exce
ption. ---> System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CPP.DivideBy0()
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.DivideBy0()
   at NDP_UE_CPP.Rethrow()

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CPP.DivideBy0()

Display the base exception using the
GetBaseException method:

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CPP.DivideBy0()
*/
// Example for the Exception.GetBaseException method.
using System;

namespace NDP_UE_CS
{
    // Define two derived exceptions to demonstrate nested exceptions.
    class SecondLevelException : Exception
    {
        public SecondLevelException( string message, Exception inner )
            : base( message, inner )
        { }
    }
    class ThirdLevelException : Exception
    {
        public ThirdLevelException( string message, Exception inner )
            : base( message, inner )
        { }
    }

    class NestedExceptions
    {
        public static void Main()
        {
            Console.WriteLine(
                "This example of Exception.GetBaseException " +
                "generates the following output." );
            Console.WriteLine(
                "\nThe program forces a division by 0, then " +
                "throws the exception \ntwice more, " +
                "using a different derived exception each time.\n" );

            try
            {
                // This function calls another that forces a
                // division by 0.
                Rethrow( );
            }
            catch( Exception ex )
            {
                Exception current;

                Console.WriteLine(
                    "Unwind the nested exceptions " +
                    "using the InnerException property:\n" );

                // This code unwinds the nested exceptions using the
                // InnerException property.
                current = ex;
                while( current != null )
                {
                    Console.WriteLine( current.ToString( ) );
                    Console.WriteLine( );
                    current = current.InnerException;
                }

                // Display the innermost exception.
                Console.WriteLine(
                    "Display the base exception " +
                    "using the GetBaseException method:\n" );
                Console.WriteLine(
                    ex.GetBaseException( ).ToString( ) );
            }
        }

        // This function catches the exception from the called
        // function DivideBy0( ) and throws another in response.
        static void Rethrow()
        {
            try
            {
                DivideBy0( );
            }
            catch( Exception ex )
            {
                throw new ThirdLevelException(
                    "Caught the second exception and " +
                    "threw a third in response.", ex );
            }
        }

        // This function forces a division by 0 and throws a second
        // exception.
        static void DivideBy0( )
        {
            try
            {
                int  zero = 0;
                int  ecks = 1 / zero;
            }
            catch( Exception ex )
            {
                throw new SecondLevelException(
                    "Forced a division by 0 and threw " +
                    "a second exception.", ex );
            }
        }
    }
}

/*
This example of Exception.GetBaseException generates the following output.

The program forces a division by 0, then throws the exception
twice more, using a different derived exception each time.

Unwind the nested exceptions using the InnerException property:

NDP_UE_CS.ThirdLevelException: Caught the second exception and threw a third in
 response. ---> NDP_UE_CS.SecondLevelException: Forced a division by 0 and thre
w a second exception. ---> System.DivideByZeroException: Attempted to divide by
 zero.
   at NDP_UE_CS.NestedExceptions.DivideBy0()
   --- End of inner exception stack trace ---
   at NDP_UE_CS.NestedExceptions.DivideBy0()
   at NDP_UE_CS.NestedExceptions.Rethrow()
   --- End of inner exception stack trace ---
   at NDP_UE_CS.NestedExceptions.Rethrow()
   at NDP_UE_CS.NestedExceptions.Main()

NDP_UE_CS.SecondLevelException: Forced a division by 0 and threw a second excep
tion. ---> System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CS.NestedExceptions.DivideBy0()
   --- End of inner exception stack trace ---
   at NDP_UE_CS.NestedExceptions.DivideBy0()
   at NDP_UE_CS.NestedExceptions.Rethrow()

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CS.NestedExceptions.DivideBy0()

Display the base exception using the GetBaseException method:

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_CS.NestedExceptions.DivideBy0()
*/
// Example for the Exception.GetBaseException method.
open System

// Define two derived exceptions to demonstrate nested exceptions.
type SecondLevelException(message, inner: Exception) =
    inherit Exception(message, inner)

type ThirdLevelException(message, inner: Exception) = 
    inherit Exception(message, inner)

printfn 
    """This example of Exception.GetBaseException generates the following output.

The program forces a division by 0, then throws the exception
twice more, using a different derived exception each time.
"""


// This function forces a division by 0 and throws a second exception.
let divideBy0 () =
    try
        let zero = 0
        let ecks = 1 / zero
        ()
    with ex ->
        raise (SecondLevelException("Forced a division by 0 and threw a second exception.", ex) )

// This function catches the exception from the called
// function divideBy0() and throws another in response.
let rethrow () =
    try
        divideBy0 ()
    with ex ->
        raise (ThirdLevelException("Caught the second exception and threw a third in response.", ex) )

try
    // This function calls another that forces a
    // division by 0.
    rethrow ()
with ex ->
    printfn "Unwind the nested exceptions using the InnerException property:\n"

    // This code unwinds the nested exceptions using the
    // InnerException property.
    let mutable current = ex
    while current <> null do
        printfn $"{current}\n"
        current <- current.InnerException

    // Display the innermost exception.
    printfn "Display the base exception using the GetBaseException method:\n"
    printfn $"{ex.GetBaseException()}"


// This example of Exception.GetBaseException generates the following output.
//
// The program forces a division by 0, then throws the exception
// twice more, using a different derived exception each time.
//
// Unwind the nested exceptions using the InnerException property:
//
// NDP_UE_FS+ThirdLevelException: Caught the second exception and threw a third in
//  response. ---> NDP_UE_FS.SecondLevelException: Forced a division by 0 and thre
// w a second exception. ---> System.DivideByZeroException: Attempted to divide by
//  zero.
//    at NDP_UE_FS.divideBy0()
//    --- End of inner exception stack trace ---
//    at NDP_UE_FS.divideBy0()
//    at NDP_UE_FS.rethrow()
//    --- End of inner exception stack trace ---
//    at NDP_UE_FS.rethrow()
//    at<StartupCode$fs>.$NDP_UE_FS.main@()
//
// NDP_UE_FS.SecondLevelException: Forced a division by 0 and threw a second excep
// tion. ---> System.DivideByZeroException: Attempted to divide by zero.
//    at NDP_UE_FS.divideBy0()
//    --- End of inner exception stack trace ---
//    at NDP_UE_FS.divideBy0()
//    at NDP_UE_FS.rethrow()
//
// System.DivideByZeroException: Attempted to divide by zero.
//    at NDP_UE_FS.divideBy0()
//
// Display the base exception using the GetBaseException method:
//
// System.DivideByZeroException: Attempted to divide by zero.
//    at NDP_UE_FS.divideBy0()
' Example for the Exception.GetBaseException method.
Namespace NDP_UE_VB

    ' Define two derived exceptions to demonstrate nested exceptions.
    Class SecondLevelException
        Inherits Exception
           
        Public Sub New( message As String, inner As Exception )
            MyBase.New( message, inner )
        End Sub
    End Class

    Class ThirdLevelException
        Inherits Exception
           
        Public Sub New( message As String, inner As Exception )
            MyBase.New( message, inner )
        End Sub
    End Class

    Class NestedExceptions
       
        Public Shared Sub Main( )
            Console.WriteLine( _
                "This example of Exception.GetBaseException " & _
                "generates the following output." )
            Console.WriteLine( vbCrLf & _
                "The program forces a division by 0, then throws " & _
                "the exception " & vbCrLf & "twice more, using " & _
                "a different derived exception each time:" & vbCrLf )
              
            Try
                ' This sub calls another that forces a division by 0.
                Rethrow()

            Catch ex As Exception
                Dim current As Exception
                 
                Console.WriteLine( _
                    "Unwind the nested exceptions using the " & _
                    "InnerException property:" & vbCrLf )
                 
                ' This code unwinds the nested exceptions using the 
                ' InnerException property.
                current = ex
                While Not ( current Is Nothing )
                    Console.WriteLine( current.ToString( ) )
                    Console.WriteLine( )
                    current = current.InnerException
                End While
                 
                ' Display the innermost exception.
                Console.WriteLine( _
                    "Display the base exception using the " & _
                    "GetBaseException method:" & vbCrLf )
                Console.WriteLine( _
                    ex.GetBaseException( ).ToString( ) )
            End Try
        End Sub
           
        ' This sub catches the exception from the called sub
        ' DivideBy0( ) and throws another in response.
        Shared Sub Rethrow( )
            Try
                DivideBy0( )

            Catch ex As Exception
                Throw New ThirdLevelException( _
                    "Caught the second exception and " & _
                    "threw a third in response.", ex )
            End Try
        End Sub
           
        ' This sub forces a division by 0 and throws a second 
        ' exception.
        Shared Sub DivideBy0( )
            Try
                Dim zero As Integer = 0
                Dim ecks As Integer = 1 \ zero

            Catch ex As Exception
                Throw New SecondLevelException( _
                    "Forced a division by 0 and threw " & _
                    "a second exception.", ex )
            End Try
        End Sub
    End Class
End Namespace ' NDP_UE_VB

' This example of Exception.GetBaseException generates the following output.
' 
' The program forces a division by 0, then throws the exception
' twice more, using a different derived exception each time:
' 
' Unwind the nested exceptions using the InnerException property:
' 
' NDP_UE_VB.ThirdLevelException: Caught the second exception and threw a third
' in response. ---> NDP_UE_VB.SecondLevelException: Forced a division by 0 and
' threw a second exception. ---> System.DivideByZeroException: Attempted to div
' ide by zero.
'    at NDP_UE_VB.NestedExceptions.DivideBy0()
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.NestedExceptions.DivideBy0()
'    at NDP_UE_VB.NestedExceptions.Rethrow()
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.NestedExceptions.Rethrow()
'    at NDP_UE_VB.NestedExceptions.Main()
' 
' NDP_UE_VB.SecondLevelException: Forced a division by 0 and threw a second exc
' eption. ---> System.DivideByZeroException: Attempted to divide by zero.
'    at NDP_UE_VB.NestedExceptions.DivideBy0()
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.NestedExceptions.DivideBy0()
'    at NDP_UE_VB.NestedExceptions.Rethrow()
' 
' System.DivideByZeroException: Attempted to divide by zero.
'    at NDP_UE_VB.NestedExceptions.DivideBy0()
' 
' Display the base exception using the GetBaseException method:
' 
' System.DivideByZeroException: Attempted to divide by zero.
'    at NDP_UE_VB.NestedExceptions.DivideBy0()

설명

예외 체인은 체인의 각 예외가 해당 속성에서 참조된 예외의 직접적인 결과로 throw되도록 예외 집합으로 구성됩니다 InnerException . 지정된 체인의 경우 체인에 있는 다른 모든 예외의 근본 원인인 단 하나의 예외가 있을 수 있습니다. 이 예외를 기본 예외라고 하며 해당 InnerException 속성에는 항상 null 참조가 포함됩니다.

예외 체인의 모든 예외에 대해 메서드는 GetBaseException 동일한 개체(기본 예외)를 반환해야 합니다.

예외의 GetBaseException 근본 원인을 찾으려 하지만 현재 예외와 첫 번째 예외 간에 발생할 수 있는 예외에 대한 정보가 필요하지 않은 경우 이 메서드를 사용합니다.

상속자 참고

GetBaseException 메서드는 예외 콘텐츠 또는 형식을 제어해야 하는 클래스에서 재정의됩니다.

적용 대상