【发布时间】:2010-07-13 17:46:29
【问题描述】:
通常,我会这样做:
try
{
code
code that might throw an anticipated exception you want to handle
code
code that might throw an anticipated exception you want to handle
code
}
catch
{
}
这样做有什么好处吗?
code
try
{
code that might throw an anticipated exception you want to handle
}
catch
{
}
code
try
{
code that might throw an anticipated exception you want to handle
}
catch
{
}
code
更新:
我最初问这个问题时参考了 C#,但正如 A. Levy 评论的那样,它可以适用于任何异常处理语言,所以我让标签反映了这一点。
【问题讨论】:
-
您希望处理在第一个错误发生时立即停止(第一个示例),还是即使部分处理失败(另一个)也可以继续?
-
@DLP - 我希望它停止处理 - 我看到有人在你之后提到它。这是一个很好的观点。
-
虽然你用 C# 和 .net 标记了它,但我认为它真的与语言无关。这个问题适用于除 Java、C++、JavaScript、OCaml、F#、Python、Clojure 等例外的任何语言。它也适用于 Common Lisp 和各种其他 Lisp 中的条件/重启系统。所以我要求你让这种语言不可知......不过你的电话。