使用try catch块的最佳实践是什么 [英] what are the best practices for using a try catch block

查看:64
本文介绍了使用try catch块的最佳实践是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好...使用try catch块的最佳实践是什么,在我的项目中,我仅对SQL执行和计算部件使用try catch块. 请帮助

Hello...What are best practices for using try catch blocks,In my project i am using try catch block for Sql Executions and Calculations parts only..is it ok???
Pls Help

推荐答案

MSDN上的相关文章:
http://msdn.microsoft.com/en-us/library/seyhszts.aspx [ ^ ]

有关此主题的热门文章,请参见CodeProject:
.NET中的异常处理最佳实践 [
Relevant article on MSDN:
http://msdn.microsoft.com/en-us/library/seyhszts.aspx[^]

Popular article on this topic here on CodeProject:
Exception Handling Best Practices in .NET[^]


最常见的破坏性错误是在catch块中:许多开发人员捕获异常并阻止其在堆栈中的进一步传播.这样的阻塞应该只在每个堆栈的最顶端执行,以防止应用程序终止,也应该在面向事件的UI的主循环中(例如在Application.Run内部),这些库提供特殊的事件和选项来处理线程异常.等级).在几乎所有其他处理异常中,都应再次抛出throw,将其重新抛出,可以选择地,可以捕获低级异常,并抛出语义感知的异常(捕获的异常应放置在新创建的异常的InnerException中) .

这些建议中的例外情况相对较少;通常,当源代码无法修复时,阻止所有或某些异常向上传播,可以解决某些代码中的某些缺陷.

—SA
Most frequent and disrupting mistake is in the catch block: many developer catch exception and blocking its further propagation up the stack. Such blocking should be done only on very top of each stack, to prevent termination of application, also in the main cycle of event-oriented UI (such as inside Application.Run, the libraries provide special events and options to handle thread exceptions on this level). In almost all other processing exception should be re-thrown again throw throw, optionally, low-level exception can be caught, and semantic-aware exception thrown (the caught exception should be placed in InnerException of the newly created exception).

Exclusions from these recommendations are relatively rare; usually, blocking of all or some exception from propagation up the stack is used as a work-around of some defects in the some code, when its source code in not available for fixes.

—SA


这篇关于使用try catch块的最佳实践是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆