


Frontier
Exception design guidelines, refer to Microsoft msdn, combine your own understanding and the handling of exception errors in past development, summarize the software development architecture, and how to better design A set of exception error guidelines.
Introduction to guidelines
Execution failure concept
The meaning of execution failure: execution failure occurs whenever a member cannot do what it was designed to do (what the member name implies). For example, if the OpenFile method cannot return an opened file handle to the caller, it would be considered an execution failure.
Translation:
The meaning of operation failure: Whenever a member module cannot complete its expected task, it is said that an operation failure has occurred. For example, the OpenFile method cannot return a handle to the open file to the caller, which is an operation failure.
Handling Exceptions in the Framework
In the Framework, exceptions are 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 prohibited when designing exceptions, which should be done without hesitation, and which should be considered, are listed in the table below.
Number | Method | How to do |
---|---|---|
1 | Return error code | Prohibited |
2 | Execution error, to Throws an exception; if OpenFile() does not return a file handle | Recommendations |
3 | If the code becomes unsafe to continue execution, Consider whether to call System.Environment.FailFast to terminate the process or throw an exception. | Consider |
4 | If possible, throw an exception at the normal control flow, see the analysis below | Prohibition |
5 | The impact of throwing exceptions on performance. | Consider |
6 | Incorporate exception handling part into the agreement | Suggestions |
7 | Return exceptions as return values | Prohibited |
8 | Use the exception generator method to avoid code bloat , use helper methods to create exceptions and properties. | Consider |
9 | Throwing exceptions in exception filters. | Disallowed |
10 | Explicitly throwing exceptions from finally blocks | Forbidden |
Explanation on Article 4:
In daily coding, consider the Tester-Doer pattern for members that may throw exceptions in common scenarios to avoid performance problems related to exceptions. The Tester-Doer pattern pides a call that might throw exceptions into two parts: a Tester and a Doer. The Tester performs a test for the state that can cause the Doer to throw an exception . The test is inserted just before the code that throws the exception, thereby guarding against the exception. Reference example from http://blog.csdn.net/troubleshooter/article/details/18401491
Code:
Tester and Doer perform their respective duties, perfectly reducing exception throwing and improving performance.
Doer: The above status monitoring is good before it can be processed by DoProcess(); if it is false, and if DoProcess() includes DoCheck() logic, an exception will be thrown, but this way After separation, DoProcess() will not throw an exception!
if(DoCheck()==true)//这是Tester:状态监测 DoProcess();
Common exceptions and handling methods in software development (own summary)
1 It is recommended to wrap the operation interface exposed by the UI layer with a try{}catch{} block, write the thrown exception to disk in catch.
2 When a timer is used in the UI layer, after an exception occurs in the counter's callback function, the timer must be stopped to prevent the error log from being written to the file.
3 The bottom layer is not recommended to wrap the try{}catch{} block. It is recommended to use throw to throw an exception directly. Because the try{} and catch{} blocks are wrapped on the UI layer, there is no It is necessary to write in these layers.
4 throw will directly interrupt future operations and jump to the outer stack try{} and catch{}, that is, the UI layer. Taking advantage of this property, it is generally recommended that functions do not return error codes.
5 When processing batch-imported data, a local exception occurred. Excel imports personnel, equipment, plans, materials, processes, etc. If a certain row of data violates the rules, it is not recommended to throw an exception at this time, because once an exception is thrown, it means that the data in the following rows cannot be imported, and the data that has been imported becomes dirty data.
Generally, there are two approaches: illegal data appears in a certain row and is recorded in the log file. In the future, based on this file, it is found that the data has not been imported, and then this one can be processed separately; Before importing, check directly After checking whether the data in all rows are legal, import one by one. Otherwise, a prompt will pop up and no data will be written to the database. It is generally recommended to do the latter. This approach is called: Tester-Doer exception mode, which is also recommended by Microsoft.
6 When processing the kanban display data, a local exception occurred. This processing mode is different from 5. Generally, when an exception occurs at this time, the former method of 5 is often adopted: displays the correct data, and writes the illegal data to the log for review; but it is also possible , if the main data in the displayed interface does not exist, an exception will be thrown directly, written to the log, and solved through the log. Therefore, it should be processed according to the abnormality severity of the data.
7 Based on development documents, logs, and analysis, try to find the reason why a certain function is not implemented. First, keep the development documents and check whether the current user requirements are consistent with those in the development documents. If they are consistent, the role of the log will be displayed at this time. For example, a pie chart summarizing the completion of all processes within a week. If there is no process data, then the pie chart may not exist. During the development process, if you check whether It does not mean that there is a process. If a process is not found, an exception may be thrown. If the process is written to the log, the reason will be found. Therefore, this type of problem should also be written to the log. Although it is not an error, it can be classified as an exception.
8 The function returns an object whose methods and properties are referenced by subsequent logic. This is inevitable! And the implementation of most functions depends on this. Because the returned object will be referenced later, It is recommended to do a null comparison. If it is null, should it be passed to the UI layer and a message prompt will pop up, or an exception will be thrown directly. The UI layer will write it to the log after processing, depending on the situation. Certainly.
The above is the detailed content of .NET Framework-detailed explanation of exception design principles. For more information, please follow other related articles on the PHP Chinese website!

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools