Using Exceptions throwing in C#. Does it affect a performance?

浪子不回头ぞ 提交于 2019-12-19 08:14:08

问题


Basically, the question is: Do the Exceptions in C# affect the performance a lot? Is it better to avoid Exceptions rethrow? If i generate an exception in my code, does it affect a performance?

Sorry for the sillines of the question itself


回答1:


Microsoft's Design Guidelines for Developing Class Libraries is a very valuable resource. Here is a relevant article:

Exceptions and Performance

I would also recommend the Framework Design Guidelines book from Microsoft Press. It has a lot of the information from the Design Guidelines link, but it is annotated by people with MS, and Anders Hejlsberg, himself. It gives a lot of insight into the "why" and "how" of the way things are.




回答2:


If you're worried about exception performance, you're using them wrong.

But yes, exceptions do affect performance.




回答3:


Raising an exception is an expensive operation in C# (compared to other operations in C#) but not enough that I would avoid doing it.

I agree with Jared, if your application is significantly slower because of raising and throwing exceptions, I would take a look at your overall strategy. Something can probably be refactored to make exception handling more efficient rather than dismissing the concept of raising exceptions in code.




回答4:


running code through a try/catch statement does not affect performance at all. The only performance hit comes if an exception is thrown ... because then the runtime has to unwind the stack and gather other information in order to populate the exception object.




回答5:


What most other folks said, plus: Don't use exceptions as part of the programming flow. In other words, don't throw an exception for something like, account.withdrawalAmount > account.balance. That is a business case.

The other biggie to look out for regarding performance is swallowing exceptions. It's a slippery slope, and once you start allowing your app to swallow exceptions, you start doing it everywhere. Now you may be allowing your app to throw exceptions that you don't know about because you are swallowing them, your performance suffers and you don't know why.




回答6:


This is not silly just I've seen it somewhere else also on SO.

The exceptions occur well, when things are really exceptional. Most of the time you re-throw the exception (may after logging) when there are not many chances of recovering from it. So it should not bother you for normal course of execution of program.




回答7:


Exceptions as its name implies are intended to be exceptional. Hence you can't expect them to have been an important target for optimisation. More often then not they don't perform well since they have other priorites such as gathering detailed info about what went wrong.




回答8:


Exceptions in .NET do affect performance. This is the reason why they should be used only in exceptional cases.



来源:https://stackoverflow.com/questions/1074868/using-exceptions-throwing-in-c-does-it-affect-a-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!