Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Sub New ( _ message As String _ )
Dim message As String Dim instance As New Exception(message)
public Exception ( string message )
public:
Exception (
String^ message
)
public Exception (
String message
)
public function Exception ( message : String )
Paramètres
- message
Message décrivant l'erreur.
NotesCe constructeur initialise la propriété Message de la nouvelle instance à l'aide du paramètre message.
Le tableau suivant montre les valeurs initiales des propriétés d'une instance de Exception.
| Propriété | Valeur |
|---|---|
| Référence null (Nothing en Visual Basic). | |
| Message | 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 spécifié par l'appelant comme paramètre pour la classe dérivée et la classe Exception de base.
' Example for the Exception( String ) constructor( String ). Imports System Imports Microsoft.VisualBasic Namespace NDP_UE_VB ' Derive an exception with a specifiable message. Class NotEvenException Inherits Exception Private Const notEvenMessage As String = _ "The argument to a function requiring " & _ "even input is not divisible by 2." Public Sub New() MyBase.New(notEvenMessage) End Sub ' New Public Sub New(auxMessage As String) MyBase.New(String.Format("{0} - {1}", _ auxMessage, notEvenMessage)) End Sub ' New End Class ' NotEvenException Module NewSExceptionDemo Sub Main() Console.WriteLine( _ "This example of the Exception( String )" & vbCrLf & _ "constructor generates the following output." ) Console.WriteLine( vbCrLf & _ "Here, an exception is thrown using the " & vbCrLf & _ "constructor of the base class." & vbCrLf ) CalcHalf(18) CalcHalf(21) Console.WriteLine(vbCrLf & _ "Here, an exception is thrown using the " & vbCrLf & _ "constructor of a derived class." & vbCrLf ) CalcHalf2(30) CalcHalf2(33) End Sub ' Main ' Half throws a base exception if the input is not even. Function Half(input As Integer) As Integer If input Mod 2 <> 0 Then Throw New Exception( String.Format( _ "The argument {0} is not divisible by 2.", _ input ) ) Else Return input / 2 End If End Function ' Half ' Half2 throws a derived exception if the input is not even. Function Half2(input As Integer) As Integer If input Mod 2 <> 0 Then Throw New NotEvenException( _ String.Format( "Invalid argument: {0}", input ) ) Else Return input / 2 End If End Function ' Half2 ' CalcHalf calls Half and catches any thrown exceptions. Sub CalcHalf(input As Integer) Try Dim halfInput As Integer = Half(input) Console.WriteLine( _ "Half of {0} is {1}.", input, halfInput ) Catch ex As Exception Console.WriteLine( ex.ToString( ) ) End Try End Sub ' CalcHalf ' CalcHalf2 calls Half2 and catches any thrown exceptions. Sub CalcHalf2( input As Integer ) Try Dim halfInput As Integer = Half2( input ) Console.WriteLine( _ "Half of {0} is {1}.", input, halfInput ) Catch ex As Exception Console.WriteLine( ex.ToString( ) ) End Try End Sub ' CalcHalf2 End Module ' NewSExceptionDemo End Namespace ' NDP_UE_VB ' This example of the Exception( String ) ' constructor generates the following output. ' ' Here, an exception is thrown using the ' constructor of the base class. ' ' Half of 18 is 9. ' System.Exception: The argument 21 is not divisible by 2. ' at NDP_UE_VB.NewSExceptionDemo.Half(Int32 input) ' at NDP_UE_VB.NewSExceptionDemo.CalcHalf(Int32 input) ' ' Here, an exception is thrown using the ' constructor of a derived class. ' ' Half of 30 is 15. ' NDP_UE_VB.NotEvenException: Invalid argument: 33 - The argument to a function ' requiring even input is not divisible by 2. ' at NDP_UE_VB.NewSExceptionDemo.Half2(Int32 input) ' at NDP_UE_VB.NewSExceptionDemo.CalcHalf2(Int32 input)
// Example for the Exception( string ) constructor. using System; namespace NDP_UE_CS { // Derive an exception with a specifiable message. class NotEvenException : Exception { const string notEvenMessage = "The argument to a function requiring " + "even input is not divisible by 2."; public NotEvenException( ) : base( notEvenMessage ) { } public NotEvenException( string auxMessage ) : base( String.Format( "{0} - {1}", auxMessage, notEvenMessage ) ) { } } class NewSExceptionDemo { public static void Main() { Console.WriteLine( "This example of the Exception( string )\n" + "constructor generates the following output." ); Console.WriteLine( "\nHere, an exception is thrown using the \n" + "constructor of the base class.\n" ); CalcHalf( 18 ); CalcHalf( 21 ); Console.WriteLine( "\nHere, an exception is thrown using the \n" + "constructor of a derived class.\n" ); CalcHalf2( 30 ); CalcHalf2( 33 ); } // Half throws a base exception if the input is not even. static int Half( int input ) { if( input % 2 != 0 ) throw new Exception( String.Format( "The argument {0} is not divisible by 2.", input ) ); else return input / 2; } // Half2 throws a derived exception if the input is not even. static int Half2( int input ) { if( input % 2 != 0 ) throw new NotEvenException( String.Format( "Invalid argument: {0}", input ) ); else return input / 2; } // CalcHalf calls Half and catches any thrown exceptions. static void CalcHalf(int input ) { try { int halfInput = Half( input ); Console.WriteLine( "Half of {0} is {1}.", input, halfInput ); } catch( Exception ex ) { Console.WriteLine( ex.ToString( ) ); } } // CalcHalf2 calls Half2 and catches any thrown exceptions. static void CalcHalf2(int input ) { try { int halfInput = Half2( input ); Console.WriteLine( "Half of {0} is {1}.", input, halfInput ); } catch( Exception ex ) { Console.WriteLine( ex.ToString( ) ); } } } } /* This example of the Exception( string ) constructor generates the following output. Here, an exception is thrown using the constructor of the base class. Half of 18 is 9. System.Exception: The argument 21 is not divisible by 2. at NDP_UE_CS.NewSExceptionDemo.Half(Int32 input) at NDP_UE_CS.NewSExceptionDemo.CalcHalf(Int32 input) Here, an exception is thrown using the constructor of a derived class. Half of 30 is 15. NDP_UE_CS.NotEvenException: Invalid argument: 33 - The argument to a function r equiring even input is not divisible by 2. at NDP_UE_CS.NewSExceptionDemo.Half2(Int32 input) at NDP_UE_CS.NewSExceptionDemo.CalcHalf2(Int32 input) */
// Example for the Exception( String* ) constructor. using namespace System; namespace NDP_UE_CPP { // Derive an exception with a specifiable message. public ref class NotEvenException: public Exception { private: static String^ notEvenMessage = "The argument to a function requiring " "even input is not divisible by 2."; public: NotEvenException() : Exception( notEvenMessage ) {} NotEvenException( String^ auxMessage ) : Exception( String::Format( "{0} - {1}", auxMessage, notEvenMessage ) ) {} }; // Half throws a base exception if the input is not even. int Half( int input ) { if ( input % 2 != 0 ) throw gcnew Exception( String::Format( "The argument {0} is not divisible by 2.", input ) ); else return input / 2; } // Half2 throws a derived exception if the input is not even. int Half2( int input ) { if ( input % 2 != 0 ) throw gcnew NotEvenException( String::Format( "Invalid argument: {0}", input ) ); else return input / 2; } // CalcHalf calls Half and catches any thrown exceptions. void CalcHalf( int input ) { try { int halfInput = Half( input ); Console::WriteLine( "Half of {0} is {1}.", input, halfInput ); } catch ( Exception^ ex ) { Console::WriteLine( ex->ToString() ); } } // CalcHalf2 calls Half2 and catches any thrown exceptions. void CalcHalf2( int input ) { try { int halfInput = Half2( input ); Console::WriteLine( "Half of {0} is {1}.", input, halfInput ); } catch ( Exception^ ex ) { Console::WriteLine( ex->ToString() ); } } } int main() { Console::WriteLine( "This example of the Exception( String* )\n" "constructor generates the following output." ); Console::WriteLine( "\nHere, an exception is thrown using the \n" "constructor of the base class.\n" ); NDP_UE_CPP::CalcHalf( 18 ); NDP_UE_CPP::CalcHalf( 21 ); Console::WriteLine( "\nHere, an exception is thrown using the \n" "constructor of a derived class.\n" ); NDP_UE_CPP::CalcHalf2( 30 ); NDP_UE_CPP::CalcHalf2( 33 ); } /* This example of the Exception( String* ) constructor generates the following output. Here, an exception is thrown using the constructor of the base class. Half of 18 is 9. System.Exception: The argument 21 is not divisible by 2. at NDP_UE_CPP.Half(Int32 input) at NDP_UE_CPP.CalcHalf(Int32 input) Here, an exception is thrown using the constructor of a derived class. Half of 30 is 15. NDP_UE_CPP.NotEvenException: Invalid argument: 33 - The argument to a function requiring even input is not divisible by 2. at NDP_UE_CPP.Half2(Int32 input) at NDP_UE_CPP.CalcHalf2(Int32 input) */
// Example for the Exception( string ) constructor. package NDP_UE_JSL; import System.* ; // Derive an exception with a specifiable message. class NotEvenException extends System.Exception { private String notEvenMessage = "The argument to a function requiring " + "even input is not divisible by 2."; public NotEvenException() { super("The argument to a function requiring " + "even input is not divisible by 2."); } //NotEvenException public NotEvenException(String auxMessage) { super(String.Format("{0} - " + "The argument to a function requiring " + "even input is not divisible by 2.", auxMessage)); } //NotEvenException } //NotEvenException class NewSExceptionDemo { public static void main(String[] args) { Console.WriteLine(("This example of the Exception( string )\n" + "constructor generates the following output.")); Console.WriteLine(("\nHere, an exception is thrown using the \n" + "constructor of the base class.\n")); CalcHalf(18); CalcHalf(21); Console.WriteLine(("\nHere, an exception is thrown using the \n" + "constructor of a derived class.\n")); CalcHalf2(30); CalcHalf2(33); } //main // Half throws a base exception if the input is not even. static int Half(int input) throws System.Exception { if (input % 2 != 0) { throw new System.Exception(String.Format( "The argument {0} is not divisible by 2.", System.Convert.ToString(input))); } else { return input / 2; } } //Half // Half2 throws a derived exception if the input is not even. static int Half2(int input) throws NotEvenException { if (input % 2 != 0) { throw new NotEvenException(String.Format("Invalid argument: {0}", System.Convert.ToString(input))); } else { return input / 2; } } //Half2 // CalcHalf calls Half and catches any thrown exceptions. static void CalcHalf(int input) { try { int halfInput = Half(input); Console.WriteLine("Half of {0} is {1}.", System.Convert.ToString(input), System.Convert.ToString(halfInput)); } catch (System.Exception ex) { Console.WriteLine(ex.toString()); } } //CalcHalf // CalcHalf2 calls Half2 and catches any thrown exceptions. static void CalcHalf2(int input) { try { int halfInput = Half2(input); Console.WriteLine("Half of {0} is {1}.", System.Convert.ToString(input), System.Convert.ToString(halfInput)); } catch (System.Exception ex) { Console.WriteLine(ex.toString()); } } //CalcHalf2 } //NewSExceptionDemo /* This example of the Exception( string ) constructor generates the following output. Here, an exception is thrown using the constructor of the base class. Half of 18 is 9. System.Exception: The argument 21 is not divisible by 2. at NDP_UE_JSL.NewSExceptionDemo.Half(Int32 input) in C:\Documents and Setting s\jitesh_chourasia\My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleAp p - JS\Class1.jsl:line 47 at NDP_UE_JSL.NewSExceptionDemo.CalcHalf(Int32 input) in C:\Documents and Set tings\jitesh_chourasia\My Documents\Visual Studio Projects\ConsoleApp - JS\Conso leApp - JS\Class1.jsl:line 72 Here, an exception is thrown using the constructor of a derived class. Half of 30 is 15. NDP_UE_JSL.NotEvenException: Invalid argument: 33 - The argument to a function r equiring even input is not divisible by 2. at NDP_UE_JSL.NewSExceptionDemo.Half2(Int32 input) in C:\Documents and Settin gs\My Documents\Visual Studio Projects\ConsoleApp - JS\ConsoleApp - JS\ Class1.jsl:line 60 at NDP_UE_JSL.NewSExceptionDemo.CalcHalf2(Int32 input) in C:\Documents and Se ttings\My Documents\Visual Studio Projects\ConsoleApp - JS\Cons oleApp - JS\Class1.jsl:line 87 */
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