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