How much efficiency does Try Catch affect the program?

Source: Internet
Author: User

Try Catch. I believe everyone has used it! In fact, I often use it because recently when I was working on a sharepoint-related project, I did not return null when calling its object method, but throw an exception message, there are a lot of try catch in the relative project! I often hear and see that Try on the Forum affects efficiency! Over time, I think so too! Today, I decided to search for related articles on the Internet!

The following is an article about Shen Shan laolin:

I remember a colleague from the company said a short time ago: "If Winform is used for development, try is used because the program is local... Catch won't have too many performance problems, but if it is on a web server, try is not recommended... Catch..., because it has a great impact on the performance of the web server ". At that time, I was always wondering whether this was the case because I did not perform tests? So I did not make a statement at that time. First, I searched through Google. There are not a few people with the same idea or questions, and there are all people who express their personal opinions. However, I personally infer that the subjective conclusion is: try... when no exception is thrown, catch does not affect the program performance, and even affects the performance, it does not necessarily become a performance bottleneck.

What is the result? Write the code and perform the test.

Try... Catch performance test code

Unexpectedly, try... Catch does not show exceptions and uses try... Catch... The average execution time ratio of exceptions does not use try... Catch... The average execution time of is short. This reminds me of a similar scene that occurred a long time ago:

I always thought that the use of transactions in SQL Server operations would be much slower than the use of transactions, but after testing, the use of transactions is much faster than the use of transactions, and it is not an order of magnitude. Therefore, using transactions becomes a means to improve database access performance.

Conclusion:

  1. No matter what development using c #, try... Catch will not affect the performance, but will slightly improve the performance.
  2. Try... Catch must be used not only in development, but also in public methods of public classes. Otherwise, an exception will affect all users who call this method together.
  3. If we do not handle exceptions in the program for the so-called performance, we cannot pass in principle ).

I used the Vs2008 + sp1 development environment for a simple Demo. If you are interested, you can download the code and test it yourself.

Event reflection:

  1. If you can do another one without using try... The catch exception test should be more helpful to this conclusion. However, after thinking about it, I am confused. If an exception occurs without processing it, there is no way to test the code execution time. It's not too early. I have to go to work today. Whether or not I will suffer from insomnia, I have to force myself to turn off my favorite computer and lie in bed.
  2. Few people are able to perform tests on such a simple problem. Generally, they are subjective expressions of their own opinions, and then they are engaged in the so-called "enterprise-level development ". If I ask such a question on the homepage of the blog garden, will there be various people competing for the opposite, their own opinions, or even scolding each other, and think that others' ideas are too naive, but is my own thoughts the truth? I think there may be at least a possibility.
  3. In the future, we will face a tit-for-tat and indecisive problem. We still hope that we will not post comments with too much confidence before we conduct investigation and testing in person. If we are not sure, we must make a statement, at least "I think" and "I personally think" should be added "......

Author: Shen Shan laolin
Source: http://wlb.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

Address: http://www.cnblogs.com/wlb/archive/2009/11/30/1613412.html

Hey hey, remember the popular songs on the Internet at the frontend:

Try if you are dead.
Song name: try to die.

Composing and singing XXX programmers

Try to die! I am not happy if I don't catch it! Program destruction throw is still in progress!
Every day is changed as a program, and all the tears have fallen,
Ignore it. The boss is good or bad. If you have a salary, you can repay the loan.
It's not that the demand is too bad. It's what SB customers want and they don't understand.
Now, I have to change the number of credit modules that I can't complete.
Try to die! I am not happy if I don't catch it! Program destruction throw is still in progress!

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.