Cette documentation est archivée et n’est pas conservée.

Exception.GetBaseException, méthode

En cas de substitution dans une classe dérivée, retourne Exception qui est à l'origine d'une ou de plusieurs exceptions suivantes.

Espace de noms : System
Assembly : mscorlib (dans mscorlib.dll)

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

Valeur de retour

Première exception levée dans une chaîne d'exceptions. Si la propriété InnerException de l'exception en cours est une référence Null (Nothing en Visual Basic), cette propriété retourne l'exception en cours.

Une chaîne d'exceptions est un jeu d'exceptions où chaque exception de la chaîne a été levée comme conséquence directe d'une exception référencée dans sa propriété InnerException. Pour une chaîne donnée, une seule exception peut être la cause première de toutes les autres exceptions figurant dans la chaîne. Cette exception est intitulée l'exception de base ; sa propriété InnerException contient toujours une référence Null.

Pour toutes les exceptions de la chaîne d'exceptions, la méthode GetBaseException doit retourner le même objet (l'exception de base).

Utilisez la méthode GetBaseException lorsque vous souhaitez trouver la cause première d'une exception sans toutefois avoir besoin d'informations sur les exceptions qui peuvent s'être produites entre l'exception en cours et la première exception.

Remarques à l'attention des héritiers La méthode GetBaseException est substituée dans les classes qui requièrent un contrôle du contenu ou du format de l'exception.

L'exemple de code suivant définit deux classes Exception dérivées. Il force une exception, puis la lève de nouveau avec chacune des classes dérivées. Le code illustre l'utilisation de la méthode GetBaseException pour récupérer l'exception d'origine.

// 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 pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

.NET Framework

Prise en charge dans : 2.0, 1.1, 1.0

.NET Compact Framework

Prise en charge dans : 2.0, 1.0
Afficher: