. NET Exception Handling Usage Analysis,. net Exception Handling usage

Source: Internet
Author: User

. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.