Export (0) Print
Expand All

Exception.GetBaseException Method

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Overridable Function GetBaseException As Exception
'Usage
Dim instance As Exception 
Dim returnValue As Exception 

returnValue = instance.GetBaseException()

Return Value

Type: System.Exception
The first exception thrown in a chain of exceptions. If the InnerException property of the current exception is a null reference (Nothing in Visual Basic), this property returns the current exception.

Implements

_Exception.GetBaseException

A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. For a given chain, there can be exactly one exception that is the root cause of all other exceptions in the chain. This exception is called the base exception and its InnerException property always contains a null reference.

For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).

Use the GetBaseException method when you want to find the root cause of an exception but do not need information about exceptions that may have occurred between the current exception and the first exception.

Notes to Inheritors:

The GetBaseException method is overridden in classes that require control over the exception content or format.

The following code example defines two derived Exception classes. It forces an exception and then throws it again with each of the derived classes. The code shows the use of the GetBaseException method to retrieve the original 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()

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft