157 recommendations for writing high-quality code to improve C # programs--recommendation 63: Avoid "eating" exceptions

Source: Internet
Author: User

Recommendation 63: Avoid "eating" exceptions

Nesting exceptions is a risky behavior, and accidentally hides the exception stack information, which is the true source of the bug. This is not the most serious, the most serious is "eat" the exception, that is, capture, and then do not throw to the upper layer.

Avoiding the "Eat" exception does not mean that you should not "eat" the exception, but there is an important principle here: The exception can be foreseen, and usually it is not considered a bug.

Imagine that you are decrypting tens of thousands of files from different clients, and that there is a case that the file is corrupted, and your goal is to insert the decrypted data into the database. This time, you have to ignore the decryption failure of the file, the whole process to go on. Of course, logging is necessary, because later you may be able to decrypt the failed files to do a unified processing.

In another case, it may not even be necessary to log logs. In distributed systems where thousands of controlled terminals are controlled, the control terminal needs to publish the heartbeat data to determine the online situation on the controlled side. The usual practice is to maintain a model quantity, if in an acceptable block time (such as 500ms) heartbeat data transmission failure, then the control thread will not receive a signal, that is, can judge the short-term status of the controlled side. In this case, it is usually meaningless to record each socketexception.

If you do not know how to handle an exception, then do not "eat" the exception, if you accidentally "eat" an exception that should be passed on, then you may be born a bug, and solve it will be a lot of trouble.

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--recommendation 63: Avoid "eating" exceptions

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.