You wrote the Try ... Is catch really necessary?

Source: Internet
Author: User

Many people like to use try ... Catch is wrapping up every method, but is it really necessary?

Why do you do this? I estimate that everyone was frightened by the bug, for fear that the production environment a variety of inexplicable errors, such as the most classic NullReferenceException, but the problem is you use try ... The catch wraps up and the error does not come out, but does the execution result be what you want? I'm afraid the bug is still there, just after your try ... Catch, the bug is more difficult to find, originally you use VS debugging, directly in the place where the exception occurred, now? You have to keep track of the past in a step.

This is not the main problem, the most important problem is that you hide the bug in the development process, if you did not add this try ... Catch, I'm afraid you've already found this bug, because the program is not going to run.

Exception information should be captured by the top-level framework, such as Exceptionfilter in MVC, where you can record detailed logs, and don't throw the yellow pages to the user. I want to write a try ... Catch scene, but incredibly suddenly can't think of a good scene, because really need to write try ... The catch scene is very few, once you want to write the try ... Catch, first think about whether you are deliberately hiding the bug.

Instead, I encourage you to write the throw exception, such as this scenario:

        Public voidRegister (stringUserName,stringpassword) {            if(string. IsNullOrEmpty (userName))Throw NewInvaliddataexception ("user name can not be empty"); if(string. IsNullOrEmpty (password))Throw NewInvaliddataexception ("Password can not be empty"); //....        }                  Public voidRegister (stringUserName,stringpassword) {            Try            {                //....            }            Catch(Exception) {}}

Theoretically, although the UI does a variety of checks, the register we write still keeps the argument untrusted, continuing to throw exceptions instead of try. Catch so that you can find the bug that the UI doesn't check in advance?

You wrote the Try ... Is catch really necessary?

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.