UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
NamespaceNettest
{
Public Class Testexception
{
Public VoidTestthrow ()
{
// The try block must be used with the catch or finally block and can contain multiple catch blocks.
Try
{
CustomexceptionEx =New Customexception("Test custom exception");
Ex. modulename ="Front-end";
ThrowEx;
}
/*
Multiple catch blocks can be connected together. The calculation sequence of multiple catch blocks is from top to bottom.
However, only one catch block is executed for each exception.
The first catch block that matches the exact type of the thrown exception or the base class is executed.
If no Catch Block matches the exception filter, the catch block without the filter will be executed (if any ).
Put the Catch Block with the most specific (I .e. the highest degree of derivation) exception class at the beginning
*/
Catch(CustomexceptionEx)
{
System.Console. Out. writeline (ex. Message +"Module is :"+ Ex. modulename );
System.Console. Out. writeline ("------------------------------");
System.Console. Out. writeline (ex. tostring ());
}
Catch(ExceptionEx)
{
System.Console. Out. writeline (ex. Message );
}
// The Finally block allowsProgramAny unclear status that may be left by the try block to be cleared by the worker,
// Or release any external resources (handle, database connection, or file Stream)
// You do not have to wait for the garbage collector in the running database to terminate these objects. The Finally block is executed in any situation.
Finally
{
// Code to execute after try (and possibly catch) here
System.Console. Out. writeline ("Test complete");
}
}
}
// Custom exception
[Serializable]
Class Customexception:Exception
{
PublicCustomexception (StringMessage ):Base(Message)
{
}
Public StringModulename {Get;Set;}
Public Override StringTostring ()
{
Return Base. Tostring () +This. Modulename. tostring ();
}
}
}