Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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)

public virtual Exception GetBaseException ()
public Exception GetBaseException ()
public function GetBaseException () : Exception

Return Value

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.

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.
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.
package NDP_UE_JSL; 

import System.* ;

// Define two derived exceptions to demonstrate nested exceptions.
class SecondLevelException extends System.Exception
{
    public SecondLevelException(String message, System.Exception inner)
    {
        super(message, inner);
    } //SecondLevelException
} //SecondLevelException

class ThirdLevelException extends System.Exception
{
    public ThirdLevelException(String message, System.Exception inner)
    {
        super(message, inner);
    } //ThirdLevelException
} //ThirdLevelException

class NestedExceptions
{
    public static void main(String[] args)
    {
        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 (System.Exception ex) {
            System.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.get_InnerException();
            }

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

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

    // This function forces a division by 0 and throws a second 
    // exception.
    static void DivideBy0() throws SecondLevelException
    {
        try {
            int zero = 0;
            int ecks = 1 / zero;
        }
        catch (System.Exception ex) {
            throw new SecondLevelException(
                "Forced a division by 0 and threw " 
                + "a second exception.", ex);
        }
    } //DivideBy0
} //NestedExceptions
   
/*
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_JSL.ThirdLevelException: Caught the second exception and threw a third in
 response. ---> NDP_UE_JSL.SecondLevelException: Forced a division by 0 and thre
w a second exception. ---> System.DivideByZeroException: Attempted to divide by
zero.
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 79
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 82
   at NDP_UE_JSL.NestedExceptions.Rethrow() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\Cl
ass1.jsl:line 65
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.NestedExceptions.Rethrow() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\Cl
ass1.jsl:line 68
   at NDP_UE_JSL.NestedExceptions.main(String[] args) in C:\Documents and 
Settings\My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleA
pp - JS\Class1.jsl:line 36

NDP_UE_JSL.SecondLevelException: Forced a division by 0 and threw a second 
exception. ---> System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 79
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 82
   at NDP_UE_JSL.NestedExceptions.Rethrow() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\Cl
ass1.jsl:line 65

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 79

Display the base exception using the GetBaseException method:

System.DivideByZeroException: Attempted to divide by zero.
   at NDP_UE_JSL.NestedExceptions.DivideBy0() in C:\Documents and Settings\
   My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\
Class1.jsl:line 79
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.