C # Try...catch...throw

Source: Internet
Author: User
Tags try catch

Try: Catch.. Throw is the mechanism of handling anomalies in C #;
When we encode, whether in business logic processing or mathematical operations, there may be unexpected situations, such as in the basic arithmetic operation, there may be overflow or other than 0 problems, then this requires a mechanism to deal with this anomaly, enhance the performance and stability of the software, try catch throw is used to handle exceptions.

A try code block generally holds the business processing logic code, such as trying to open a database connection, when processing business operations, and so on, all the code inside the try code, once an unknown exception occurs, the code in the try block is terminated, but instead jumps into the catch, if not placed inside the try, Once an exception occurs, the program faces a crash.

Catch code block and try match appear to handle exceptions that occur in a try block.
You can have multiple catch after a try to perform the most matching one based on the exception type in the catch (exception type);
Can not follow catch after try, but must have followed finally (can also follow finally when there is catch on the try)

Finally: The code in the code block is definitely executed regardless of the code execution or exception, so you can put some code that closes the database connection or frees up memory here.

When do I use a try? Try using more may be effective to avoid the system crashes due to an exception, but excessive use of try will reduce system performance, to introduce you to the general process of try in the program run:
When the program runs to the start of a try, it holds an entry point for the address record in the stack, which is overhead when the try executes to find the entry point.

Give you a suggestion:
When designing to operations such as operations, logic processing, and business operations, you must use exception handling, which is not recommended when defining or instantiating variables.

C # Try...catch...throw

Related Article

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.