. NET Exception Handling Usage Analysis,. net Exception Handling usage
This article analyzes in detail how to handle exceptions and exceptions in. NET. Share it with you for your reference. The specific analysis is as follows:
Exception in. NET)
In. net, the Exception parent class is Exception, and most exceptions generally inherit from Exception.
You can customize the Exception class by writing a class that inherits the Exception!
Exception Handling Mechanism:
Copy codeThe Code is as follows: Try
{
// Code that may cause exceptions
// Subsequent Code
}
// Code other than Try
Catch (Exception e)
{
}
Finally
{
}
The above code is described as follows:
1. Who can execute
In exception handling, once there is a problem in try, the program will discard the subsequent code of the exception and directly jump to catch.
After running the code in catch, continue to execute code other than Try.
2. About the catch () parameter e
E is the exception class object, which can be named at will. It is not necessarily called e.
3. Only one
The code in a try can only throw one exception.
Why?
This is because an exception is thrown and will not be executed later!
4. Get information
You can use e. Message to obtain exception information.
5. Required
Finally will execute
6. No catch is allowed.
Only try catch is allowed.
Or try finally.
Excellent exception handling style
1. do not escape the problem. Do not just catch the exception and do nothing, or simply print it. This is not an excellent "Exception Handling" style.
If you do not know how to handle exceptions, do not catch them. Expose it. If an exception occurs, the problem must be solved. It is not a solution to escape the problem. Especially in hierarchical projects. This will cause the program to be in a deep logical chaos. The problem is hidden, and you don't even know where the problem has occurred.
2. What should I do if I really encounter a programmer with a try or catch?
Vs is very powerful. It thinks of this situation. Click debug> exception to enter such a tool and select the triggering option for the second line.
In this way, exceptions are exposed whether or not try catch is used during debugging. We can find the desired information.
I hope this article will help you with the. NET program design.