Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Sub New ( _ message As String, _ innerException As Exception _ )
Dim message As String Dim innerException As Exception Dim instance As New Exception(message, innerException)
public Exception ( string message, Exception innerException )
public:
Exception (
String^ message,
Exception^ innerException
)
public Exception (
String message,
Exception innerException
)
public function Exception ( message : String, innerException : Exception )
Paramètres
- message
Message d'erreur expliquant la raison de l'exception.
- innerException
Exception qui est à l'origine de l'exception actuelle, ou référence null (Nothing en Visual Basic) si aucune exception interne n'est spécifiée.
NotesUne exception levée comme conséquence directe d'une exception précédente doit contenir une référence à l'exception précédente dans la propriété InnerException. La propriété InnerException retourne la même valeur que celle passée dans le constructeur, ou une référence null (Nothing en Visual Basic) si la propriété InnerException ne fournit pas la valeur de l'exception interne au constructeur.
Le tableau suivant montre les valeurs initiales des propriétés d'une instance de Exception.
| Propriété | Valeur |
|---|---|
| InnerException | Référence à l'exception interne. |
| Chaîne du message d'erreur. |
ExempleL'exemple de code suivant dérive un Exception pour une condition spécifique. Le code illustre l'utilisation du constructeur qui prend un message et une exception interne comme paramètres pour la classe dérivée et la classe Exception de base.
' Sample for Exception( String, Exception ) constructor. Imports System Imports Microsoft.VisualBasic Namespace NDP_UE_VB ' Derive an exception with a specifiable message and inner exception. Class LogTableOverflowException Inherits Exception Private Const overflowMessage As String = _ "The log table has overflowed." Public Sub New( ) MyBase.New( overflowMessage ) End Sub ' New Public Sub New( auxMessage As String ) MyBase.New( String.Format( "{0} - {1}", _ overflowMessage, auxMessage ) ) End Sub ' New Public Sub New( auxMessage As String, inner As Exception ) MyBase.New( String.Format( "{0} - {1}", _ overflowMessage, auxMessage ), inner ) End Sub ' New End Class ' LogTableOverflowException Class LogTable Public Sub New( numElements As Integer ) logArea = New String( numElements ) { } elemInUse = 0 End Sub ' New Protected logArea( ) As String Protected elemInUse As Integer ' The AddRecord method throws a derived exception ' if the array bounds exception is caught. Public Function AddRecord( newRecord As String ) As Integer Try Dim curElement as Integer = elemInUse logArea( elemInUse ) = newRecord elemInUse += 1 Return curElement Catch ex As Exception Throw New LogTableOverflowException( String.Format( _ "Record ""{0}"" was not logged.", newRecord ), ex ) End Try End Function ' AddRecord End Class ' LogTable Module OverflowDemo ' Create a log table and force an overflow. Sub Main() Dim log As New LogTable(4) Console.WriteLine( _ "This example of the Exception( String, Exception )" & _ vbCrLf & "constructor generates the following output." ) Console.WriteLine( vbCrLf & _ "Example of a derived exception " & vbCrLf & _ "that references an inner exception:" & vbCrLf ) Try Dim count As Integer = 0 Do log.AddRecord( _ String.Format( _ "Log record number {0}", count ) ) count += 1 Loop Catch ex As Exception Console.WriteLine( ex.ToString( ) ) End Try End Sub ' Main End Module ' OverflowDemo End Namespace ' NDP_UE_VB ' This example of the Exception( String, Exception ) ' constructor generates the following output. ' ' Example of a derived exception ' that references an inner exception: ' ' NDP_UE_VB.LogTableOverflowException: The log table has overflowed. - Record " ' Log record number 5" was not logged. ---> System.IndexOutOfRangeException: In ' dex was outside the bounds of the array. ' at NDP_UE_VB.LogTable.AddRecord(String newRecord) ' --- End of inner exception stack trace --- ' at NDP_UE_VB.LogTable.AddRecord(String newRecord) ' at NDP_UE_VB.OverflowDemo.Main()
// Example for the Exception( string, Exception ) constructor. using System; namespace NDP_UE_CS { // Derive an exception with a specifiable message and inner exception. class LogTableOverflowException : Exception { const string overflowMessage = "The log table has overflowed."; public LogTableOverflowException( ) : base( overflowMessage ) { } public LogTableOverflowException( string auxMessage ) : base( String.Format( "{0} - {1}", overflowMessage, auxMessage ) ) { } public LogTableOverflowException( string auxMessage, Exception inner ) : base( String.Format( "{0} - {1}", overflowMessage, auxMessage ), inner ) { } } class LogTable { public LogTable( int numElements ) { logArea = new string[ numElements ]; elemInUse = 0; } protected string[ ] logArea; protected int elemInUse; // The AddRecord method throws a derived exception // if the array bounds exception is caught. public int AddRecord( string newRecord ) { try { logArea[ elemInUse ] = newRecord; return elemInUse++; } catch( Exception ex ) { throw new LogTableOverflowException( String.Format( "Record \"{0}\" was not logged.", newRecord ), ex ); } } } class OverflowDemo { // Create a log table and force an overflow. public static void Main() { LogTable log = new LogTable( 4 ); Console.WriteLine( "This example of the Exception( string, Exception )" + "\nconstructor generates the following output." ); Console.WriteLine( "\nExample of a derived exception " + "that references an inner exception:\n" ); try { for( int count = 1; ; count++ ) { log.AddRecord( String.Format( "Log record number {0}", count ) ); } } catch( Exception ex ) { Console.WriteLine( ex.ToString( ) ); } } } } /* This example of the Exception( string, Exception ) constructor generates the following output. Example of a derived exception that references an inner exception: NDP_UE_CS.LogTableOverflowException: The log table has overflowed. - Record "Lo g record number 5" was not logged. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at NDP_UE_CS.LogTable.AddRecord(String newRecord) --- End of inner exception stack trace --- at NDP_UE_CS.LogTable.AddRecord(String newRecord) at NDP_UE_CS.OverflowDemo.Main() */
// Example for the Exception( String*, Exception* ) constructor. using namespace System; namespace NDP_UE_CPP { // Derive an exception with a specifiable message and inner exception. public ref class LogTableOverflowException: public Exception { private: static String^ overflowMessage = "The log table has overflowed."; public: LogTableOverflowException() : Exception( overflowMessage ) {} LogTableOverflowException( String^ auxMessage ) : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ) ) {} LogTableOverflowException( String^ auxMessage, Exception^ inner ) : Exception( String::Format( "{0} - {1}", overflowMessage, auxMessage ), inner ) {} }; public ref class LogTable { public: LogTable( int numElements ) { logArea = gcnew array<String^>(numElements); elemInUse = 0; } protected: array<String^>^logArea; int elemInUse; public: // The AddRecord method throws a derived exception // if the array bounds exception is caught. int AddRecord( String^ newRecord ) { try { logArea[ elemInUse ] = newRecord; return elemInUse++; } catch ( Exception^ ex ) { throw gcnew LogTableOverflowException( String::Format( "Record \"{0}\" was not logged.", newRecord ),ex ); } } }; // Create a log table and force an overflow. void ForceOverflow() { LogTable^ log = gcnew LogTable( 4 ); try { for ( int count = 1; ; count++ ) { log->AddRecord( String::Format( "Log record number {0}", count ) ); } } catch ( Exception^ ex ) { Console::WriteLine( ex->ToString() ); } } } int main() { Console::WriteLine( "This example of the Exception( String*, Exception* )\n" "constructor generates the following output." ); Console::WriteLine( "\nExample of a derived exception " "that references an inner exception:\n" ); NDP_UE_CPP::ForceOverflow(); } /* This example of the Exception( String*, Exception* ) constructor generates the following output. Example of a derived exception that references an inner exception: NDP_UE_CPP.LogTableOverflowException: The log table has overflowed. - Record "L og record number 5" was not logged. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at NDP_UE_CPP.LogTable.AddRecord(String newRecord) --- End of inner exception stack trace --- at NDP_UE_CPP.LogTable.AddRecord(String newRecord) at NDP_UE_CPP.ForceOverflow() */
// Example for the Exception( string, Exception ) constructor. package NDP_UE_JSL; import System.* ; // Derive an exception with a specifiable message and inner exception. class LogTableOverflowException extends System.Exception { private String overflowMessage = "The log table has overflowed."; public LogTableOverflowException() { super("The log table has overflowed."); } //LogTableOverflowException public LogTableOverflowException(String auxMessage) { super(String.Format("The log table has overflowed. - {0}", auxMessage)); } //LogTableOverflowException public LogTableOverflowException(String auxMessage, Exception inner) { super(String.Format("The log table has overflowed.- {0}", auxMessage), inner); } //LogTableOverflowException } //LogTableOverflowException class LogTable { public LogTable(int numElements) { logArea = new String[numElements]; elemInUse = 0; } //LogTable protected String logArea[]; protected int elemInUse; // The AddRecord method throws a derived exception // if the array bounds exception is caught. public int AddRecord(String newRecord) throws LogTableOverflowException { try { logArea.set_Item(elemInUse, newRecord); return elemInUse++; } catch (Exception ex) { throw new LogTableOverflowException(String.Format( "Record \"{0}\" was not logged.", newRecord), ex); } } //AddRecord } //LogTable class OverflowDemo { // Create a log table and force an overflow. public static void main(String[] args) { LogTable log = new LogTable(4); Console.WriteLine(("This example of the Exception( string, Exception )" + "\nconstructor generates the following output.")); Console.WriteLine(("\nExample of a derived exception " + "that references an inner exception:\n")); try { for(int iCtr = 1; ; iCtr++) { log.AddRecord(String.Format("Log record number {0}", System.Convert.ToString(iCtr))); } } catch (System.Exception ex) { Console.WriteLine(ex.toString()); } } //main } //OverflowDemo /* This example of the Exception( string, Exception ) constructor generates the following output. Example of a derived exception that references an inner exception: NDP_UE_JSL.LogTableOverflowException: The log table has overflowed.- Record "Log record number 5" was not logged. ---> java.lang.ArrayIndexOutOfBoundsException: Index was outside the bounds of the array. --- End of inner exception stack trace --- at NDP_UE_JSL.LogTable.AddRecord(String newRecord) in C:\ Documents and Settings\My Documents\Visual Studio Projects\ConsoleApp - JS\ ConsoleApp - JS\Class1.jsl:line 52 at NDP_UE_JSL.OverflowDemo.main(String[] args) in C:\Documents and Settings\ My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\ Class1.jsl:line 71 */
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, 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