Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Sub New
Dim instance As New Exception
public Exception ()
public:
Exception ()
public Exception ()
public function Exception ()
NotesCe constructeur initialise la propriété Message de la nouvelle instance en lui assignant un message système qui décrit l'erreur et prend en compte la culture du système en cours.
Toutes les classes dérivées doivent fournir ce constructeur par défaut. 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 | Description localisée fournie par le système. |
ExempleL'exemple de code suivant dérive un Exception qui utilise un message prédéfini. Le code illustre l'utilisation du constructeur sans paramètre pour la classe dérivée et la classe Exception de base.
' Example for the Exception( ) constructor. Imports System Imports Microsoft.VisualBasic Namespace NDP_UE_VB ' Derive an exception with a predefined message. Class NotEvenException Inherits Exception Public Sub New( ) MyBase.New( _ "The argument to a function requiring " & _ "even input is not divisible by 2." ) End Sub ' New End Class ' NotEvenException Module NewExceptionDemo Sub Main( ) Console.WriteLine( _ "This example of the Exception( ) constructor " & _ "generates the following output." ) Console.WriteLine( vbCrLf & _ "Here, an exception is thrown using the " & vbCrLf & _ "parameterless constructor of the base class." & _ vbCrLf ) CalcHalf( 12 ) CalcHalf( 15 ) Console.WriteLine(vbCrLf & _ "Here, an exception is thrown using the " & vbCrLf & _ "parameterless constructor of a derived class." & _ vbCrLf ) CalcHalf2( 24 ) CalcHalf2( 27 ) 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( ) 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( ) 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 ' NewExceptionDemo End Namespace ' NDP_UE_VB ' This example of the Exception( ) constructor generates the following output. ' ' Here, an exception is thrown using the ' parameterless constructor of the base class. ' ' Half of 12 is 6. ' System.Exception: Exception of type System.Exception was thrown. ' at NDP_UE_VB.NewExceptionDemo.Half(Int32 input) ' at NDP_UE_VB.NewExceptionDemo.CalcHalf(Int32 input) ' ' Here, an exception is thrown using the ' parameterless constructor of a derived class. ' ' Half of 24 is 12. ' NDP_UE_VB.NotEvenException: The argument to a function requiring even input i ' s not divisible by 2. ' at NDP_UE_VB.NewExceptionDemo.Half2(Int32 input) ' at NDP_UE_VB.NewExceptionDemo.CalcHalf2(Int32 input)
// Example for the Exception( ) constructor. using System; namespace NDP_UE_CS { // Derive an exception with a predefined message. class NotEvenException : Exception { public NotEvenException( ) : base( "The argument to a function requiring " + "even input is not divisible by 2." ) { } } class NewExceptionDemo { public static void Main() { Console.WriteLine( "This example of the Exception( ) constructor " + "generates the following output." ); Console.WriteLine( "\nHere, an exception is thrown using the \n" + "parameterless constructor of the base class.\n" ); CalcHalf( 12 ); CalcHalf( 15 ); Console.WriteLine( "\nHere, an exception is thrown using the \n" + "parameterless constructor of a derived class.\n" ); CalcHalf2( 24 ); CalcHalf2( 27 ); } // Half throws a base exception if the input is not even. static int Half( int input ) { if( input % 2 != 0 ) throw new Exception( ); 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( ); 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( ) constructor generates the following output. Here, an exception is thrown using the parameterless constructor of the base class. Half of 12 is 6. System.Exception: Exception of type System.Exception was thrown. at NDP_UE_CS.NewExceptionDemo.Half(Int32 input) at NDP_UE_CS.NewExceptionDemo.CalcHalf(Int32 input) Here, an exception is thrown using the parameterless constructor of a derived class. Half of 24 is 12. NDP_UE_CS.NotEvenException: The argument to a function requiring even input is not divisible by 2. at NDP_UE_CS.NewExceptionDemo.Half2(Int32 input) at NDP_UE_CS.NewExceptionDemo.CalcHalf2(Int32 input) */
// Example for the Exception( ) constructor. using namespace System; namespace NDP_UE_CPP { // Derive an exception with a predefined message. public ref class NotEvenException: public Exception { public: NotEvenException() : Exception( "The argument to a function requiring " "even input is not divisible by 2." ) {} }; // Half throws a base exception if the input is not even. int Half( int input ) { if ( input % 2 != 0 ) throw gcnew Exception; 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; 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( ) constructor " "generates the following output." ); Console::WriteLine( "\nHere, an exception is thrown using the \n" "parameterless constructor of the base class.\n" ); NDP_UE_CPP::CalcHalf( 12 ); NDP_UE_CPP::CalcHalf( 15 ); Console::WriteLine( "\nHere, an exception is thrown using the \n" "parameterless constructor of a derived class.\n" ); NDP_UE_CPP::CalcHalf2( 24 ); NDP_UE_CPP::CalcHalf2( 27 ); } /* This example of the Exception( ) constructor generates the following output. Here, an exception is thrown using the parameterless constructor of the base class. Half of 12 is 6. System.Exception: Exception of type System.Exception was thrown. at NDP_UE_CPP.Half(Int32 input) at NDP_UE_CPP.CalcHalf(Int32 input) Here, an exception is thrown using the parameterless constructor of a derived class. Half of 24 is 12. NDP_UE_CPP.NotEvenException: 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() constructor. package NDP_UE_JSL ; import System.* ; // Derive an exception with a predefined message. class NotEvenException extends System.Exception { public NotEvenException() { super("The argument to a function requiring " + "even input is not divisible by 2."); } //NotEvenException } //NotEvenException class NewExceptionDemo { public static void main(String[] args) { Console.WriteLine(("This example of the Exception( ) constructor " + "generates the following output.")); Console.WriteLine(("\nHere, an exception is thrown using the \n" + "parameterless constructor of the base class.\n")); CalcHalf(12); CalcHalf(15); Console.WriteLine(("\nHere, an exception is thrown using the \n" + "parameterless constructor of a derived class.\n")); CalcHalf2(24); CalcHalf2(27); } //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(); } 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(); } 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 } //NewExceptionDemo /* This example of the Exception( ) constructor generates the following output. Here, an exception is thrown using the parameterless constructor of the base class. Half of 12 is 6. System.Exception: Exception of type 'System.Exception' was thrown. at NDP_UE_JSL.NewExceptionDemo.Half(Int32 input) in C:\Documents and Settings \My Documents\Visual Studio\Projects\ConsoleApp - JS\ConsoleApp - JS\Class1.jsl:line 33 at NDP_UE_JSL.NewExceptionDemo.CalcHalf(Int32 input) in C:\Documents and Sett ings\My Documents\Visual Studio\Projects\ConsoleApp - JS\Consol eApp - JS\Class1.jsl:line 59 Here, an exception is thrown using the parameterless constructor of a derived class. Half of 24 is 12. NDP_UE_JSL.NotEvenException: The argument to a function requiring even input is not divisible by 2. at NDP_UE_JSL.NewExceptionDemo.Half2(Int32 input) in C:\Documents and Setting s\My Documents\Visual Studio\Projects\ConsoleApp - JS\ConsoleAp p - JS\Class1.jsl:line 46 at NDP_UE_JSL.NewExceptionDemo.CalcHalf2(Int32 input) in C:\Documents and Set tings\My Documents\Visual Studio\Projects\ConsoleApp - JS\Conso leApp - JS\Class1.jsl:line 74 */
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