. NET exceptions and exception handling

Source: Internet
Author: User
Tags try catch

. NET exceptions (Exception)

The parent class of the exception in. NET is exception, and most exceptions generally inherit from exception.

You can customize the exception class by writing a class that inherits from Exception!

exception handling mechanism

Try

{

Code that might have occurred an exception

Subsequent code

}

Code outside of Try

catch (Exception e)

{

}

Finally

{

}

The above code is described below

1. who can perform in exception handling, once a try has a problem, the program discards the exception's subsequent code and jumps directly into the catch.

executes the code in the catch and resumes execution code other than the try .

2. about the parameter in catch () e

E is the exception class object that occurs, can be arbitrarily named. Not must be called E.

3. throw only one

The code in a try can only throw an exception.

Why is it?

Because once the exception is thrown, ah, there is no implementation of the back!

4. access to information

Exception information can be obtained by e.message

5. must perform

Finally it will be executed anyway

6. can not catch

Can be only try Catch

You can also have only try finally

excellent style of exception handling

1. Don't run away from the problem don't just catch the exception, do nothing, or just print it, this is not a good "exception handling" style.

Do not catch if you do not know how to handle the exception. Expose him to it. Since there is an exception, the problem is certain, escape is not a way to face him, to solve him. Especially in layered projects. Causes the program to fall into a state of deep logic chaos. And the problem is hidden, and you don't even know where it's going to go wrong.

2. What if you really encounter a random try, catch programmer?

VS is very powerful, and it's thinking of this. Click "Debug" "Exception" to enter such a tool, select the second row of the Raise option.

This way, when debugging, whether or not the try catch will burst out of the exception. The information we wanted was found.

. NET exceptions and exception handling

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.