Exception.InnerException-Eigenschaft

Ruft die Exception-Instanz ab, die die aktuelle Ausnahme ausgelöst hat.

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

Syntax

'Declaration
Public ReadOnly Property InnerException As Exception
'Usage
Dim instance As Exception
Dim value As Exception

value = instance.InnerException
public Exception InnerException { get; }
public:
virtual property Exception^ InnerException {
    Exception^ get () sealed;
}
/** @property */
public final Exception get_InnerException ()
public final function get InnerException () : Exception

Eigenschaftenwert

Eine Instanz von Exception, die den Fehler beschreibt, der die aktuelle Ausnahme verursacht hat. Die InnerException-Eigenschaft gibt den gleichen Wert zurück, der an den Konstruktor übergeben wurde, bzw. einen NULL-Verweis (Nothing in Visual Basic), wenn der Wert der inneren Ausnahme nicht an den Konstruktor übergeben wurde. Diese Eigenschaft ist schreibgeschützt.

Hinweise

Wenn eine Ausnahme X als unmittelbares Ergebnis einer vorhergehenden Ausnahme Y ausgelöst wird, muss die InnerException-Eigenschaft von X einen Verweis auf Y enthalten.

Über die InnerException-Eigenschaft kann die Gruppe von Ausnahmen abgerufen werden, die zur aktuellen Ausnahme geführt hat.

Sie können eine neue Ausnahme erstellen, die eine frühere Ausnahme abfängt. Der Code, der die zweite Ausnahme behandelt, kann die zusätzlichen Informationen der früheren Ausnahme dazu verwenden, den Fehler auf geeignetere Weise zu behandeln.

Angenommen, eine Funktion liest eine Datei und formatiert die Daten in der Datei. In diesem Beispiel wird eine IOException ausgelöst, wenn der Code versucht, die Datei zu lesen. Die Funktion fängt die IOException ab und löst eine FileNotFoundException aus. Die IOException könnte in der InnerException-Eigenschaft von FileNotFoundException gespeichert werden, sodass Code, der die FileNotFoundException abfängt, die Ursache des ursprünglichen Fehlers ermitteln kann.

Die InnerException-Eigenschaft, die einen Verweis auf die innere Ausnahme enthält, wird bei der Initialisierung des Ausnahmeobjekts festgelegt.

Beispiel

Das folgende Beispiel veranschaulicht des Auslösen und Abfangen einer Ausnahme, die auf eine innere Ausnahme verweist.

Imports System

Public Class MyAppException
   Inherits ApplicationException
   
   Public Sub New(message As [String])
      MyBase.New(message)
   End Sub 'New
   
   Public Sub New(message As [String], inner As Exception)
      MyBase.New(message, inner)
   End Sub 'New
End Class 'MyAppException

Public Class ExceptExample
   
   Public Sub ThrowInner()
      Throw New MyAppException("ExceptExample inner exception")
   End Sub 'ThrowInner
   
   Public Sub CatchInner()
      Try
         Me.ThrowInner()
      Catch e As Exception
         Throw New MyAppException("Error caused by trying ThrowInner.", e)
      End Try
   End Sub 'CatchInner
End Class 'ExceptExample

Public Class Test
   
   Public Shared Sub Main()
      Dim testInstance As New ExceptExample()
      Try
         testInstance.CatchInner()
      Catch e As Exception
         Console.WriteLine("In Main catch block. Caught: {0}", e.Message)
         Console.WriteLine("Inner Exception is {0}", e.InnerException)
      End Try
   End Sub 'Main
End Class 'Test
using System;
public class MyAppException:ApplicationException 
{
   public MyAppException (String message) : base (message) 
   {}
   public MyAppException (String message, Exception inner) : base(message,inner) {} 
   }
public class ExceptExample 
{
   public void ThrowInner () 
   {
   throw new MyAppException("ExceptExample inner exception");
   }
   public void CatchInner() 
   {
      try 
      {
      this.ThrowInner();
      }
         catch (Exception e) 
         {
         throw new MyAppException("Error caused by trying ThrowInner.",e);
         }
      }
   }
public class Test 
{
   public static void Main() 
   {
   ExceptExample testInstance = new ExceptExample();
      try 
      {
      testInstance.CatchInner();
      }
         catch(Exception e) 
         {
         Console.WriteLine ("In Main catch block. Caught: {0}", e.Message);
         Console.WriteLine ("Inner Exception is {0}",e.InnerException);
         }
      }
}
using namespace System;
public ref class MyAppException: public ApplicationException
{
public:
   MyAppException( String^ message )
      : ApplicationException( message )
   {}

   MyAppException( String^ message, Exception^ inner )
      : ApplicationException( message, inner )
   {}

};

public ref class ExceptExample
{
public:
   void ThrowInner()
   {
      throw gcnew MyAppException( "ExceptExample inner exception" );
   }

   void CatchInner()
   {
      try
      {
         this->ThrowInner();
      }
      catch ( Exception^ e ) 
      {
         throw gcnew MyAppException( "Error caused by trying ThrowInner.",e );
      }

   }

};

int main()
{
   ExceptExample^ testInstance = gcnew ExceptExample;
   try
   {
      testInstance->CatchInner();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "In Main catch block. Caught: {0}", e->Message );
      Console::WriteLine( "Inner Exception is {0}", e->InnerException );
   }

}
import System.*;
public class MyAppException extends ApplicationException
{
    public MyAppException(String message)
    {
      super(message);
    } //MyAppException

    public MyAppException(String message, System.Exception inner)
    {
      super(message, inner);
    } //MyAppException
} //MyAppException

public class ExceptExample
{
    public void ThrowInner()throws MyAppException
    {
        throw new MyAppException("ExceptExample inner exception");
    } //ThrowInner

    public void CatchInner()throws MyAppException
    {
        try {
            this.ThrowInner();
        }
        catch (System.Exception e) {
            throw new MyAppException("Error caused by trying ThrowInner.", e);
        }
    } //CatchInner
} //ExceptExample

public class Test
{
    public static void main(String[] args)
    {
        ExceptExample testInstance = new ExceptExample();
        try {
            testInstance.CatchInner();
        }
        catch (System.Exception e) {
            Console.WriteLine("In main catch block. Caught: {0}", 
                e.get_Message());
            Console.WriteLine("Inner Exception is {0}", 
                e.get_InnerException());
        }
    } //main
} //Test

Dieser Code erzeugt die folgende Ausgabe:

In Main
  catch block. Caught: Error caused by trying ThrowInner. Inner Exception is
  MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at
  ExceptExample.CatchInner()

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Exception-Klasse
Exception-Member
System-Namespace