General Catch Block

Source: Internet
Author: User

Public void mymothed ()
{
Try
{
// Do something
Int x = 5/0; // It is divided by 0. An error occurs here.
}
Catch (systemexception)
{
// Do something here
}
Catch (dividebyzeroexception)
{
// Here is the solution to the error of division by 0
}
Catch (exception)
{
// This is a general Catch Block"
}
}

In the above example, the last "catch (exception)" is "General Catch Block", which is responsible for capturing exceptions, the exception type indicates "errors occurred during application execution. ", That is to say, no matter what error occurs, this catch can catch it. All error types in C # are derived from this exception class, and the C # syntax specifies a try .. only one common catch block can be found.

In addition, the catch (exception) block must be placed in a try .. at the end of the catch structure, otherwise a compiler warning will be generated, because this block will capture all the errors, so the subsequent catch statements will never be executed.

 

 

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.