. Exception and exception handling usage analysis in net-practical tips

Source: Internet
Author: User
Tags exception handling try catch

This paper is analyzed in detail. NET exception and exception handling usage. Share to everyone for your reference. The specific analysis is as follows:

. NET exception (Exception)

The parent class of the exception in. NET is exception, and most exceptions inherit from exception.
You can customize the exception class by writing a class that inherits from Exception!

Exception handling mechanism:

Copy Code code as follows:
Try
{
Code that may have an exception
Subsequent code
}
Code outside of Try
catch (Exception e)
{
}
Finally
{
}

The above code describes the following

1. Who can perform

In exception handling, once there is a problem in the try, the program discards the subsequent code of the exception and jumps directly into the catch.
Executes the code in the catch and continues to execute code other than try.

2. About the parameter E in catch ()
e is an exception to the exception class object, can be arbitrarily named. It's not an e to be called.

3. Throw only one
the code in a try can only throw an exception.
Why, then?
Because once the exception is thrown, Ah, the back is not carried out AH!

4. Access to Information
can get exception information through E.message

5. The need to implement
Finally, it will be done anyway.

6. There can be no catch
you can have only try Catch
Or you can only try and finally

Excellent exception handling style

1. Do not evade the problem do not just catch the exception to live, do nothing, or just print, this is not a good "exception handling" style.
Don't catch the exception if you don't know how to handle it. Let him be exposed. Since the exception has occurred, the problem must be some, escape is not a way to face him, to solve his. Especially in layered projects. can cause the program to fall into a deep, logical state of confusion. And the problem is hidden, and you don't even know what's going on.

2. What if you really encounter a random try, catch the programmer?
VS is powerful, it thinks of this. Click on "Debug" "Exception" to enter such a tool, select the second row of the Raise option.

In this way, when debugging, whether or not try catch will be a sudden leakage of the exception. The information we wanted was found.

I hope this article will be described to you. NET program design helps.

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.