Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the situations of exception handling in .NET?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the situations of .NET exception handling". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what are the situations of .NET exception handling"?

Net should keep in mind the strategy of exception handling from beginning to end: throw a specific exception instead of just throwing an exception of type Exception, which makes it easy for us to catch exceptions of the corresponding type. When writing code, we should take into account the worst-case scenario of the application; display good information and provide appropriate administrator contact information.

1. Do not throw "new Exception ()"

Please don't do this. Exception is a very abstract exception class, and catching such exceptions usually has a lot of negative effects. In general, we should define our own exception classes, and we need to distinguish between the exceptions thrown by the system and those thrown by ourselves.

2. Do not store important exception information in the Message attribute

Exceptions are encapsulated in classes. When you need to return abnormal information, please store the information in separate properties (not in the Message property), otherwise it will be difficult for people to parse the information they need from the Message property. For example, when you just need to correct a spelling mistake, and if you String the error message and other prompts in the Message property, how can others simply get the error message they want? You can hardly imagine how much effort they have to make.

3. Each thread should contain a try/catch block

General exception handling is placed in a more concentrated place in the program. Each thread needs to have a try/catch block, otherwise you will miss some exceptions and cause incomprehensible problems. When a program opens multiple threads to handle background tasks, you usually create a type to store the results of each thread's execution. At this point, don't forget to add a field to the type to store possible exceptions for each thread, otherwise the main thread will not know about the exceptions of other threads. In some "send-and-forget" situations, you may need to copy a copy of the exception handling logic from the main thread to your child thread.

4. Record the exception after catching it.

It doesn't matter how your program logs-log4net, EIF, Event Log, TraceListeners, text files, etc. The important thing is that when you encounter an exception, you should record it in the log somewhere. But please record it only once, otherwise you will end up with a very large log file that contains a lot of duplicate information.

5. Do not only record the value of Exception.Message, but also record Exception.ToString ()

When we talk about logging, don't forget that we should record the value of Exception.ToString (), not Exception.Message. Because Exception.ToString () contains "stack trace" information, internal exception information, and Message. Usually this information is very important, but if you only record the Exception.Message, you will only see a prompt like "the object reference does not point to the instance in the heap."

6. To catch specific exceptions

If you want to catch exceptions, try to catch specific exceptions (not Exception) as much as possible. Good code can only catch exceptions that it knows how to handle.

Don't forget to use using

It is not enough to simply call the object's Dispose () method. The using keyword prevents resource leaks even when an exception occurs

8. Do not use special return values to represent exceptions that occur in a method

1) it is faster to throw an exception directly, because when we use a special return value to represent an exception, we need to check the return result every time we call the method, and this takes up at least one more register. Slow down the code.

2), the special return value can, and is likely to be ignored

3) the special return value cannot contain stack trace information and cannot return the details of the exception

4) in many cases, there is no special value to represent the exception that occurs in the method, for example, when the divisor is zero

9. Do not "throw an exception" as a result of function execution

This is a very bad design. Too many try/catch blocks in the code will make the code difficult to understand. Proper design can satisfy a method to return a variety of different execution results. If you really need to represent the execution results of the method by throwing an exception, it only means that your method has done too much and must be split.

10. You can use the method of "throwing an exception" to highlight errors that cannot be ignored

For example, I have developed an API (Login) for one of my products to log in. If the user fails to log in, or if the user does not call the Login method, then they will fail to call other methods. I did this when I designed the Login method: if the user fails to log in, it throws an exception instead of simply returning false. Because of this, the caller (user) will not ignore the fact that he has not logged in yet.

Ps: four elements of .net exception handling

1. A class that represents exception details.

two。 A member who throws an exception class instance like the caller.

3. The caller's segment invokes the module of the exception member.

4. A section of the caller that handles or captures a block of code that is about to cause an exception.

Thank you for your reading. The above is the content of "what are the situations of .NET exception handling?" after the study of this article, I believe you have a deeper understanding of the situation of .NET exception handling. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report