开发者

Performance Tips try/catch blocks

In ASP.NET I would like your opinion regarding what is the best way for Handling Ex开发者_开发问答ceptions.

  • Would you suggest to use try/catch blocks?
  • Better to use try/catch blocks on every single piece of code/component? Or in which situations?


You should definitely not wrap every piece of code in try/catch blocks. You should only catch the exceptions you can actually handle at any given point. All other exceptions should be allowed to propagate.

One of the benefits of exceptions is that you don't need to handle each and every error locally. By using try/catch all over the place, you actively work against this concept.


Use try finally anytime you have resources like connections that need to be cleaned up. But use try catch ONLY if you can actually do something about an exception. Otherwise let it bubble up the stack and catch it in the global asax so you can fail gracefully.


Throw Fewer Exceptions : You might want to read stackoverflow thread


Throwing exceptions is the best way to know where you application is going wrong.

As a side note the most annoying thing for a developer supporting a large system is trying to work out why something is not falling over but is not performing correctly and it's because someone has caught an error somewhere in business process and just made the code go ssshhhhh.


Try to keep try/catch blocks to a minimum, as they create performance overhead. Only use a try/catch/finally when you're able to safely correct the state of the application when the exception has occurred (e.g., closing connections, etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜