In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to handle exceptions in the program in dotNET. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Usually in the process of software development, the first thing is to ensure that the function can run normally and meet the business needs. In addition, we also need to consider how to deal with the code when it is abnormal, so that the program can run healthily. Handling exceptions correctly and reasonably can reduce the Bug of the program and ensure the quality of the code, which is certainly not an easy task.
In our daily work, we often encounter some problems when troubleshooting errors. If not, it means you are doing a good job:
Want to analyze the cause of the error by the way of log, and find that the log record is incomplete.
Found the error log, recorded "the object reference is not set to the instance of the object", and also know the number of lines of code, however, there are multiple reference types of objects on this line, still do not know the real reason
The problem is occasional and cannot be repeated.
In the end, you need to restore the database for single-step debugging to solve the problem, however:
The customer's database is classified and cannot be provided.
The customer's database has been running for many years, and the amount of data is so large that it can not be backed up and restored quickly.
If it is an Internet Saas application, it is even more difficult to get the library locally for debugging.
Therefore, it needs to be optimized at the code level and the log level to achieve the purpose of quickly locating the problem.
DotNET Classic error
The above picture is no stranger to programmers who have experienced the era of dotNET Framework, which is the classic "Yellow pages" and the classic "object reference not set to the object instance" error.
First of all, this error display is very unfriendly, except to let people know that this is developed by dotNET, there is no other use, and this error prompt is not helpful to troubleshooting errors, only know that the object is null, but the reason is not known, can only guess, can guess depends on luck.
Correct thinking of error handling
A system is generally used by two types of people, ordinary users and system administrators. Both ordinary users and system administrators expect feedback on all operations in the operating system, either returning the desired results normally or giving friendly error prompts that can guide the next step.
When an exception occurs, you can navigate to a dedicated type of error prompt page, or you can pop up the error prompt in a modal manner, including:
Error message, for example: system exception, please contact the administrator, dial xxx, save failed, please contact the administrator
Global error code, which will be discussed below
For exception coding, you can quickly query the log record in the background according to this code. You can use date plus serial number for exception coding. It is recommended not to use Guid, which has been regarded as garbled by non-technical personnel.
If it is a function used by the system administrator, I think it is also possible to display the real cause of the error in the error prompt.
Global error code
By setting the global error code, administrators can quickly locate the problem and find a solution according to the error code when they receive the feedback error. Therefore, it is necessary to have a public global error code document to record the cause of the error and solution reference.
In general, it can be divided into 4xx and 5xxPower4xx to represent the front-end parameter problems, verification problems and so on, while 5xx represents the back-end logic problems.
You can subdivide further in the 5xx type, for example:
500100: indicates problems related to database operation
500200: indicates that the list shows related questions
Wait
Some principles of exception handling
1. Do not return an error code in the method, because the information of the error code is too single
2. Select a specific exception type when throwing an exception, instead of throwing a System.Exception directly.
3. the purpose of the error message is to enable developers to locate and solve problems, rather than showing them to end users, and the information shown to front-end users should be friendly and easy to understand.
4. Do not swallow exceptions. For example, do not do any processing after catch exception. If some resources need to be cleaned, you can use try. Finally or use using
5. Only when you know how to recover from an exception, do you need to catch an exception. When performing some operations, we may know the cause of the error, but we cannot recover, so don't catch the exception.
How do I handle exceptions in a method?
There are three parts in a method: parameters, business logic, and return values
Parameters.
For the parameters of the reference type, a non-null judgment must be made at the beginning of the method, and whether to throw an exception or continue the following logic should be determined according to the specific situation:
If the parameter is null, it will affect the subsequent business. You should throw an exception.
If we can do some initialization after judging null, so that the program can continue to run normally, and ensure that the business is correct, there is no need to throw an exception.
Business logic
The part of the business logic is divided into three situations:
Call a method of other types inside the method, such as var user= userService.GetUser (); the judgment of user, whether to throw an exception when it is null, is consistent with the logic of the above parameters
After multiple logics are combined to judge, if the next input cannot be satisfied, an exception should be thrown.
For calls at a lower level, exceptions are sometimes caught. When an exception is caught, proprietary exception information in accordance with the current context should be thrown, which is more conducive to locating the problem.
Return value
The return value of a method can return a value type, such as string, int, bool, or a reference type. For example, a User object is returned, no matter what type is returned, the principle is the same, and it needs to be judged in more context.
There is a GetUser method to get the user object. If the user is not found according to Id, you can return null directly instead of returning an empty User object. If an empty object is returned, the program will not make an error, but there is no data in the front-end display, so it is not clear whether the user is not found or found but has no value; if you return null, it is up to the upper layer to decide how to handle it.
Then there is a GetUserList method that gets the set of users based on the criteria, and if no matching user is found based on the search criteria, you can return an empty object List instead of null.
The same is true for value types, depending on the context. For example, the function IndexOf in C #, which is used to find the index position of characters in a string, returns int type, and returns-1 instead of null when it cannot be found.
The above is how to handle exceptions in the program in dotNET. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.