Msdn: Try-catch

Source: Internet
Author: User

The try-catch statement consistsTryBlock and one or moreCatchClause (specify processing for different exceptions)Program. This statement takes one of the following forms:

 
Try Try-blockCatch (Exception-declaration-1) Catch-block-1Catch (Exception-declaration-2) Catch-block-2...Try Try-blockCatch Catch-block

Where:

Try-block
Include exceptions that should be thrown Code.
Exception-Declaration, Exception-declaration-1, Exception-declaration-2
Exception object declaration.
Catch-block, Catch-block-1, Catch-block-2
Contains the exception handling program.

Example
In this example, two catch statements are used. The most specific exception that occurs first is caught.
// Ordering catch clauses
Using system;
Class myclass
{
Public static void main ()
{
Myclass x = new myclass ();
Try
{
String S = NULL;
X. myfn (s );
}

// Most specific:
Catch (argumentnullexception E)
{
Console. writeline ("{0} first exception caught.", e );
}

// Least specific:
Catch (exception E)
{
Console. writeline ("{0} second exception caught.", e );
}

}

Public void myfn (string S)
{
If (S = NULL)
Throw new argumentnullexception ();
}
}

Output

System. argumentnullexception: value cannot be null.
At...
At... first exception caught.

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.