Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic ReadOnly Property InnerException As Exception
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
Valeur de la propriété
Instance de Exception décrivant l'erreur qui a provoqué l'exception en cours. La propriété InnerException retourne la même valeur que celle qui a été passée dans le constructeur, ou une référence Null (Nothing en Visual Basic) si la valeur de l'exception interne n'a pas été fournie au constructeur. Cette propriété est en lecture seule.
NotesLorsqu'une exception X est levée comme conséquence directe d'une exception précédente Y, la propriété InnerException de X doit comprendre une référence à Y.
Utilisez la propriété InnerException pour obtenir le jeu d'exceptions ayant provoqué l'exception en cours.
Vous pouvez créer une nouvelle exception qui intercepte une exception antérieure. Le code qui gère la deuxième exception peut utiliser les informations supplémentaires de l'exception antérieure pour gérer l'erreur de manière plus appropriée.
Soit une fonction lisant un fichier et mettant en forme les données de ce fichier. Dans cet exemple, IOException est levée lorsque le code essaie de lire le fichier. Cette fonction intercepte IOException et lève FileNotFoundException. IOException peut être enregistré dans la propriété InnerException de FileNotFoundException, ce qui permet au code qui intercepte FileNotFoundException d'examiner la cause de l'erreur initiale.
La propriété InnerException, qui contient une référence à l'exception interne, est définie lors de l'initialisation de l'objet exception.
ExempleL'exemple suivant illustre la levée et l'interception d'une exception qui fait référence à une exception interne.
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
Ce code génère la sortie suivante :
In Main catch block. Caught: Error caused by trying ThrowInner. Inner Exception is MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at ExceptExample.CatchInner()
Plates-formesWindows 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.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ