C # collection series (8): exception

Source: Internet
Author: User

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 ();

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.