Detailed introduction to the design Guidelines for exceptions (Except) in. NET and other schemas

Source: Internet
Author: User
Tags finally block

Frontier

Exception design guidelines, referring to Microsoft MSDN, combined with their own understanding and the past development of the exception of the processing, summarize the software development framework, how to better design a set of abnormal error criteria.

Introduction guidelines

Execution failure concept

The meaning of execution failure: Execution failure occurs whenever a member cannot do what it is designed to do (What the member name implies). For example, if the OpenFile method cannot return a opened file handle to the caller, it would is considered an execution Failure.

Translation:

The operation failed: whenever a member module fails to complete its intended task, it is called an operation failure. For example, the OpenFile method cannot return a handle to an open file to caller, which is a failed operation.

Handling Exceptions in schemas

In the Framework, exceptions is used for all error conditions, including execution errors.

Translation:

In the framework, exceptions are used to handle all error conditions, including execution errors.

Summary guidelines

Which methods should be banned when designing exceptions, which should be without hesitation to do, and which need to be considered, are listed in the table below.

The effect of td> returns an exception as a return value An exception was thrown in the
numbering methods procedure
1 return error code Forbidden
2 execution error, to throw an exception, such as OpenFile () did not return a file handle recommendations
3 If the code resumes execution, When it becomes unsafe, consider whether to call System.Environment.FailFast to terminate the process or throw an exception. consider
4 If possible, throw an exception at normal control flow, see analysis below no
5 throwing exceptions on performance. consider
6 contract to include exception handling section recommendations
7 suppress
8 Use the exception generator method to avoid code bloat and to create exceptions and properties with helper methods. consider
9 exception filter. Forbidden
to throw an exception from a finally block no

  
The 4th article explains:

In daily coding, consider the tester-doer pattern for members and may throw exceptions in common scenarios to AV OID performance problems related to exceptions. The tester-doer pattern Pides a call this might throw exceptions into the parts: a Tester and a doer. The Tester performs a test for the state so can cause the doer to throw a exception. The test is inserted just before the code that throws the exception, thereby guarding against the exception. From http://www.php.cn/

Refer to the example code:

Tester and Doer perform their duties, perfectly reducing abnormal throws and improving performance.
Doer: The above condition monitoring is good to doprocess () processing, if False, and if Doprocess () includes Docheck () logic, an exception is thrown, but when separated, doprocess () will not throw an exception!

if (Docheck () ==true)//This is tester: condition monitoring    doprocess ();

Common exceptions and handling methods of software development (self-summary)

1 UI layer exposed interface, recommended package try{}catch{} block , catch will throw exception written to disk.

2 When a timer is used in the UI layer, after the counter's callback function has an exception, stop the timer and prevent the error log from being written into the file.

3 The underlying recommendation does not wrap try{}catch{} blocks , it is recommended to throw an exception directly with throw, because the UI layer wraps the try{} and catch{} blocks, so there is no need to write on these layers.

4 throw will directly interrupt the operation later, jump to the enclosing stack outer package try{} and catch{}, that is, the UI layer, using this nature, generally recommended function do not return error code .

5 A local exception occurred while processing bulk-imported data. Excel importer, device, plan, material, process, etc. if a row of data is violated, it is not recommended to throw an exception, because once an exception is thrown, it means that the data in the subsequent row is not imported and has been imported into dirty data.
Generally there are two ways: a row of violations of data, recorded in the log file, in the future according to the file to find that the data is not imported, and then handle this alone can be, before the import, directly check all rows of data is legitimate, check the correct, then one by one import , or direct pop-up prompts, No data can be written to the database. The latter procedure is generally recommended . This practice is known as: tester-doer anomaly mode, and Microsoft's recommended practice.

6 Handling Kanban Presentation data, local exception. This processing mode is different from 5, generally at this time abnormal, often take 5 of the former practice: to display the correct data, the violation of data written to the log, left to view ; but it is also possible that if the display interface, the main data does not exist, then directly throw an exception, write to the log, Resolved through the log. Therefore, we should deal with the abnormal severity of the data.

7 According to the development of the document, log, analysis, as far as possible to find a function is not implemented reasons . The first thing to do is to keep a good development document and see if the user's requirements are consistent with the development documentation. If consistent, this time the role of the log is displayed, for example, the summary of all operations in a week to complete the pie chart, if an operation data is not, then the pie chart may not have, in the development process, if the check is not an operation, if not found an operation, may throw an exception, and then write to the log to the word, Reason will be found. Therefore, this type of problem is also written to the log, although it is not an error, but can be classified as an exception.

The 8 function returns an object whose methods and properties are referenced by subsequent logic. This is inevitable! And most of the implementation of the functionality is dependent on this. This object is returned because of the subsequent reference, so it is recommended to do a null comparison, if NULL, is passed to the UI layer, pop-up message prompts, or directly throws an exception, the UI layer is processed and written to the log, depending on the situation.

This is the detailed description of the exception (EXCEPT) design guidelines in. NET and other architectures, and more about topic.alibabacloud.com (www.php.cn)!

  • 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.