Make the program more robust, use errors or throw exceptions?

Source: Internet
Author: User

Make the program more robust, use errors or throw exceptions? [Transfer]

 

I remember a long time ago, when I used C # as the main work content, I discussed how to handle exceptions well with my colleagues and my colleagues in cnblogs several times. At that time, I came to the conclusion that exception handling wasProgramNormal operation does not have a significant impact on efficiency. It is only slightly affected when the program throws and captures the exception's validity rate.

 

Today, we will not talk about efficiency. We will only discuss how to improve program robustness.

Based on previous experience, exception handling should follow the following rules:

1. Capture exceptions on the UI Layer and ignore exceptions that can be ignored. Handle non-negligible exceptions and send them to users.

2. Try not to handle exceptions on the BL layer. If you need to handle exceptions, ensure that the exceptions do not affect the normal operation of the system.

3. If you can use custom exceptions, use custom exceptions. Otherwise, make sure to catch (exception)

 

 

If the above three rules are followed and exceptions are well handled, our program will greatly improve its robustness.

 

If the framework you are using is not a try exception method but a custom error method to improve program robustness, once any exception is thrown, and you didn't pass this exception to your custom error in time. Congratulations, you don't have to worry about doing anything in the future! Because everything is an error class you defined, and if the corresponding business logic is not met during business logic processing, you will put the corresponding information in the error, then, the other layer is required to capture the error when calling this method and process it ...... Imagine if a newbie comes to your project team and is not clear about your processing logic, it will not get your error or handle it !! OMG ...... Let's wait for some bugs on the production machine that are inexplicable and cannot be found ......

 

If you are a framework designer, be sure to carefully consider how to enable people who use the Framework to capture errors at the bottom of your framework in the future, we also need to consider that the program designed with your framework can run normally and run smoothly every day.

 

If you are a framework user who uses "errors" instead of "exceptions", and you have the ability to transform your framework, this is the best thing, if you cannot transform the framework you are using, try to make the program you write more robust even if it is poorly designed. Do not make the program you write too fragile ......

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.