Dieser Artikel wurde manuell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. |
Übersetzung
Original
|
Exception.GetBaseException-Methode
Gibt beim Überschreiben in einer abgeleiteten Klasse eine Exception zurück, die die ursprüngliche Ursache für eine oder mehrere nachfolgende Ausnahmen ist.
Assembly: mscorlib (in mscorlib.dll)
Rückgabewert
Typ: System.ExceptionDie erste Ausnahme, die in einer Kette von Ausnahmen ausgelöst wird. Wenn die InnerException-Eigenschaft der aktuellen Ausnahme ein NULL-Verweis (Nothing in Visual Basic) ist, gibt diese Eigenschaft die aktuelle Ausnahme zurück.
Implementiert
_Exception.GetBaseException()Als Kette von Ausnahmen wird eine Reihe von Ausnahmen bezeichnet, bei der jede Ausnahme als direkte Folge der Ausnahme, auf die ihre InnerException-Eigenschaft verweist, ausgelöst wurde. In einer Kette gibt es nur eine einzige Ausnahme, die die ursprüngliche Ursache aller anderen Ausnahmen ist. Diese Ausnahme wird als Basisausnahme bezeichnet. Ihre InnerException-Eigenschaft enthält immer einen NULL-Verweis.
Die GetBaseException-Methode muss für alle Ausnahmen in der Kette von Ausnahmen dasselbe Objekt (die Basisausnahme) zurückgeben.
Verwenden Sie die GetBaseException-Methode, wenn Sie die ursprüngliche Ursache suchen, jedoch keine Informationen über Ausnahmen benötigen, die zwischen der ersten und der aktuellen Ausnahme ausgelöst wurden.
Hinweise zur Vererbung
Die GetBaseException-Eigenschaft wird in Klassen überschrieben, die den Inhalt oder das Format von Ausnahmen steuern müssen.
Im folgenden Codebeispiel werden zwei abgeleitete Exception-Klassen definiert. Eine Ausnahme wird erzwungen, die dann noch einmal mit beiden abgeleiteten Klassen ausgelöst wird. Der Code veranschaulicht die Verwendung der GetBaseException-Methode zum Abrufen der ursprünglichen Ausnahme.
// 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() */
Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.