次の方法で共有


Exception.GetBaseException メソッド

派生クラスでオーバーライドされた場合、それ以後に発生する 1 つ以上の例外の主要な原因である Exception を返します。

Public Overridable Function GetBaseException() As Exception
[C#]
public virtual Exception GetBaseException();
[C++]
public: virtual Exception* GetBaseException();
[JScript]
public function GetBaseException() : Exception;

戻り値

例外のチェインでスローされた最初の例外。現在の例外の InnerException プロパティが null 参照 (Visual Basic の場合は Nothing) である場合、このプロパティは現在の例外を返します。

解説

例外のチェインを構成するそれぞれの例外は、 InnerException プロパティで参照された例外の直接的な結果としてスローされたものです。チェインには、そのチェイン内の他のすべての例外の主要原因である例外が 1 つだけ含まれている場合があります。この例外は基本例外と呼ばれ、その InnerException プロパティには必ず null 参照が含まれています。

GetBaseException メソッドは、例外のチェイン内のすべての例外に対して、同じオブジェクト (基本例外) を返す必要があります。

ある例外の主要原因を見つける必要があるが、その例外から現在の例外までの間に発生した例外に関する情報は不要な場合には、 GetBaseException メソッドを使用します。

継承時の注意: GetBaseException メソッドは、例外の内容または書式に対する制御が必要なクラスではオーバーライドされます。

使用例

[Visual Basic, C#, C++] 2 つの派生 Exception クラスを定義するコード例を次に示します。この例では、例外を強制し、その例外を各派生クラスで再度スローします。 GetBaseException メソッドを使用して元の Exception を取得します。

 
' Example for the Exception.GetBaseException method.
Imports System
Imports Microsoft.VisualBasic

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 ' New
    End Class ' SecondLevelException

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

    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 ' Main
           
        ' 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 ' Rethrow
           
        ' 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 ' DivideBy0
    End Class ' NestedExceptions
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()

[C#] 
// 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()
*/

[C++] 
// Example for the Exception::GetBaseException method.
#using <mscorlib.dll>
using namespace System;

namespace NDP_UE_CPP
{
    // Define two derived exceptions to demonstrate nested exceptions.
    __gc class SecondLevelException : public Exception
    {
    public: 
        SecondLevelException( String* message, Exception* inner ) :
            Exception( message, inner )
        { }
    };
    __gc 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 new SecondLevelException( 
                S"Forced a division by 0 and threw "
                S"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 new ThirdLevelException( 
                S"Caught the second exception and "
                S"threw a third in response.", ex );
        }
    }
}

void main() 
{
    Console::WriteLine( 
        S"This example of Exception.GetBaseException " 
        S"generates the following output." );
    Console::WriteLine( 
        S"\nThe program forces a division by 0, "
        S"then throws the exception \ntwice more, " 
        S"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( 
            S"Unwind the nested exceptions using "
            S"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( 
            S"Display the base exception using the \n"
            S"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()
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Exception クラス | Exception メンバ | System 名前空間