C# 在 try catch 中使用 try catch 好不好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13759759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it good to use try catch within a try catch?
提问by HotTester
Possible Duplicate:
Are nested Try/Catch blocks a bad idea?
可能的重复:
嵌套的 Try/Catch 块是个坏主意吗?
Currently I am using try catch within try catch ? The current senario requires it in our application.
目前我在 try catch 中使用 try catch ?当前 senario 在我们的应用程序中需要它。
void MyFun()
{
try
{
//Process logic 1
// ......
try
{
//Process logic 2
// ......
} catch (Exception ex)
{
//write an error details in database
}
//Process Logic 3
// ......
} catch (Exception ex)
{
//show error msg
}
}
回答by Chadwick13
This item suggests that its not a bad thing and that you would only have to handle the error in another way any way.
此项表明这不是一件坏事,您只需以其他方式处理错误即可。
回答by Itay Karo
No particular problem with this especially if you want to handle the exceptions differently.
However, if the inner exception and the outer exception are of different types E1, E2respectively and E1is not a parent of E2, you can have two adjacent catch clauses.
这没有特别的问题,特别是如果您想以不同的方式处理异常。
但是,如果内部异常和外部异常分别属于不同类型E1, E2并且E1不是 的父级E2,则可以有两个相邻的 catch 子句。
try
{
// do something
}
catch (E1 e1)
{
}
catch (E2 e2)
{
}
As noted by Rob and J.Steen - this is slightly different than the case in the question as in this case is E1is thrown the code after it will not be executed.
正如 Rob 和 J.Steen 所指出的 - 这与问题中的情况略有不同,因为在这种情况下E1,代码在不会执行后被抛出。
回答by The Unculled Badger
Its a little difficult to answer this question without knowing what logic is in here. Certainly in terms of performance, nested exception handling will incur a greater cost, but general rule of thumb is only catch exceptions that you as a developer understand how to handle. This overlaps into the practice of TDD where if you have a good enough set of tests you can identify where the expected exceptions should be, then this will dictate your exception logic.
在不知道这里有什么逻辑的情况下,回答这个问题有点困难。当然,在性能方面,嵌套异常处理会产生更大的成本,但一般的经验法则是只捕获您作为开发人员了解如何处理的异常。这与 TDD 的实践重叠,如果您有足够好的测试集,您可以确定预期的异常应该在哪里,那么这将决定您的异常逻辑。
回答by Rob
I would say this: it's not bad. Whether it's gooddepends on your program, and whether such a concept makes sense given your method's logic and contracts.
我会说:这还不错。它是否好取决于你的程序,以及考虑到你的方法的逻辑和契约,这样的概念是否有意义。
Edit: I'd suggest checking out the article Exception Cost: When to throw and when not to. It outlines what is most expensive for exception management in the CLR.
编辑:我建议查看文章异常成本:何时抛出和何时不抛出。它概述了 CLR 中异常管理最昂贵的内容。
回答by Faster Solutions
I don't see why not. If you have logic in the catch which may fail or raise an exception that requires handling then it makes sense.
我不明白为什么不。如果您在 catch 中有逻辑可能会失败或引发需要处理的异常,那么这是有道理的。
回答by jb.
A nested try/catch is fine. what you want to stay away from is changing the logical flow of your code based on the try catch. In other words, you shouldn't treat a try/catch as an if/else block. so this isn't ideal:
嵌套的 try/catch 很好。您想要远离的是根据 try catch 更改代码的逻辑流程。换句话说,您不应该将 try/catch 视为 if/else 块。所以这并不理想:
//over the top example just to demonstrate my point
public bool IsNumberTen(int x)
{
try
{
if(x > 10)
throw new NumberTooHighException();
else if(x < 10)
throw new NumberTooLowException();
else
return true;
}
catch(NumberTooHighException)
{
return false;
}
catch(NumberTooLowException)
{
return false;
}
}
回答by beyond-code
I am trying to think of situations where you may want a nested block... perhaps if you are making database changes and you are using the try catch as a virtual transaction, you may want to try to update some properties but then carry on if that fails, but also catch an overall exception if and when you actually commit to the database update itself.
我正在尝试考虑您可能需要嵌套块的情况......也许如果您正在更改数据库并且您将 try catch 作为虚拟事务使用,您可能想要尝试更新某些属性,但如果失败,但如果您实际提交数据库更新本身,也会捕获整体异常。
Even with this considered, you should never need to do this... It should be perfectly sufficient to simply stack blocks next to each other like so:
即使考虑到这一点,你也永远不需要这样做......像这样简单地将块堆叠在一起就足够了:
void MyFun()
{
try
{
//Process logic 1
// ......
} catch (Exception ex)
{
//show error msg
}
try
{
//Process logic 2
// ......
} catch (Exception ex)
{
//write an error details in database
}
}
}
It is also probably worth noting that if you find the need to nest try catch blocks then there is probably a better way you could be designing your code.
还可能值得注意的是,如果您发现需要嵌套 try catch 块,那么可能有更好的方法来设计您的代码。
EDIT:Itay's answer is also somewhat better than nesting, although it will not allow you to carry on in the block once you have caught an exception.
编辑:Itay 的答案也比嵌套好一些,尽管它不允许您在捕获异常后继续执行。
Hope this helps!
希望这可以帮助!

