Exception.GetBaseException 方法

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Function GetBaseException As Exception
用法
Dim instance As Exception
Dim returnValue As Exception

returnValue = instance.GetBaseException
public virtual Exception GetBaseException ()
public:
virtual Exception^ GetBaseException ()
public Exception GetBaseException ()
public function GetBaseException () : Exception

返回值

异常链中第一个被引发的异常。如果当前异常的 InnerException 属性是空引用(Visual Basic 中为 Nothing),则此属性返回当前异常。

备注

异常链由这样一组异常组成:该链中的每个异常都是作为其 InnerException 属性中所引用异常的直接结果而引发的。对于一个给定的链,只能有一个异常是该链中其他所有异常的根源。此异常称为基异常,其 InnerException 属性总是包含空引用。

对于一个异常链中的所有异常,GetBaseException 方法必须返回同一对象(基异常)。

当需要查找异常的根源,但又不需要当前异常和第一个异常之间可能发生的异常的信息时,可以使用 GetBaseException 方法。

给继承者的说明 在需要控制异常内容或格式的类中重写 GetBaseException 方法。

示例

下面的代码示例定义两个 Exception 派生类。它强行产生一个异常,然后用每个派生类再次引发该异常。代码演示如何使用 GetBaseException 方法检索原始异常。

' 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()
// 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.
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.
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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Exception 类
Exception 成员
System 命名空间