157 recommendations for writing high-quality code to improve C # programs--Recommendation 70: Avoid logging exceptions at lower call stacks

Source: Internet
Author: User

Recommendation 70: Avoid logging exceptions where the call stack is lower

Not all exceptions are recorded to the log, in which case the scenario where the exception occurred needs to be recorded, and the other is the uncaught exception. An uncaught exception is usually treated as a bug, so the record for it should be considered an important part of the system.

The best fit for logging exceptions and reports is the top level of the application, which is typically the UI layer. Assuming that an application exists, its BLL layer may be called by a WinForm form, or it may be called by a console application, and when the BLL module reports an exception to the administrator, You might not know whether to use the MessageBox method or use the Console.Write method.

If the exception is logged or reported at a lower call stack, and there are cases that are thrown after being wrapped:

        Internal void Methodlow ()        {            try            {                //...             }            catch  (someexception)            {                logger.write ("  Exception someexception occurs ")                throw;            }        }

The record exception is also captured at the higher call stack location:

        Internal void Methodhigh ()        {            try            {                methodlow ();            }             Catch (someexception)            {                logger.write (" exception someexception occurs ")            }        }

This will cause the record to recur. In the case of low call stacks, exceptions are often caught and cannot be processed completely. Therefore, in a comprehensive consideration, the application should, in the early stages of design, be expected to record and report exceptions for development members.

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 70: Avoid logging exceptions at lower call stacks

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.