During this time, I found some good things when preparing for. Net error handling. Here I will share with you.
The exception handling of. NET is actually very similar to that of Java, with GC (Garbage Collector ). In the early days of VB/C ++, there seems to be no garbage collection, and we are all aware of the advantages of GC. So how should we use GC for more efficient programming?
To be honest, this part of the content is not very well organized and easy to understand. I have been sorting out these contents over and over the past few days, and I have spent a lot of effort. Later I thought about his mechanism, but it would be better to learn more and use it better.
Before that, let's talk about some basic concepts.
Errors: errors in a program are generally divided into syntax errors, logical errors, and runtime errors. During Exception Handling, errors mainly refer to runtime errors, that is, problems occurred during the execution of compiled programs.
Exception: an error occurs. The object created at the run time is used to handle the error.
It should be emphasized that an error refers to an event, and an exception is an object created to process this event.
Here are some good practices or Exception Handling principles.
1. Check as soon as possible.
The earlier the problem is discovered, the easier it will be to solve.
2. Do not trust external data
All external data should be checked (databases, files, keyboards, etc)
3. Try not to throw a new exception ()
Exception is a very large class that contains a large amount of information
Make the thrown exceptions as targeted as possible to provide meaningful error information.
4. Record exceptions
If you capture an exception, you must record it somewhere. Do not record only information in. Message, but all error information.
5. Do not swallow exceptions
The worst thing you do is to add an empty module after catch (exception.
6. Using is frequently used.
Even if an exception occurs, the keyword using prevents resource leakage. For example
using(SqlConnection conn = new SqlConnection()) { //do something; }
7. Do not use exception handling methods as a means to return information from functions (do not use exception control procedures)
Not only is Exception Handling slow, but many try/catch modules in the Code make the code difficult to maintain.
8. Use exceptions for errors that should not be ignored
If there are certain requirements or prerequisites for method execution, you should use exceptions so that the calling program cannot ignore them (for example, some methods require user information)
9. Do not clear stack tracing when an exception is thrown again.
Try {
}
Catch (exception ex)
{
// Incorrect method
Throw ex;
}
Try {
}
Catch (exception ex)
{
// The correct method
Throw;
}
Here it is said that an exception is thrown again after it is caught. The first case is an error because, in the first case, a new exception is thrown, the original exception information is overwritten. In the second case, the caught exception information will be retained, and a new error message will be added based on the original error to throw.
10. The exception should be identified by [serializable]
Exceptions are serialized in most cases. When inheriting from another exception class, do not forget to add this attribute.
I have also learned many articles from Daniel, and I feel very useful, so I will share them with you. If you have any shortcomings or better insights, I would like to raise more!