C# Try catch finally: 如果没有抛出异常,就做点什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10431413/
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
Try catch finally: Do something if no exception is thrown
提问by lowerkey
I'm wondering, is there a way to only execute a block if no exception was thrown?
我想知道,有没有办法只在没有抛出异常的情况下执行块?
The best I can come up with is this:
我能想到的最好的是:
bool exception = false;
try{
// something
}catch(Exception e){
exception = true;
}finally{
if(!exception){
// i can do what i want here
}
}
Is there a better way?
有没有更好的办法?
采纳答案by Jon
Sure there is: put it at the bottom of the tryblock.
当然有:把它放在try块的底部。
try{
// something
// i can do what i want here
}catch(Exception e){
// handle exception
}
This is not entirely equivalent to your original code in the sense that if "what you want" throws, the exception will be caught locally (this would not happen with your original scheme). This is something you might or might not care about, and there's a good chance that the different behavior is also the correct one.
这并不完全等同于您的原始代码,如果“您想要的”抛出,异常将在本地捕获(这不会发生在您的原始方案中)。这是您可能会或可能不会关心的事情,并且很有可能不同的行为也是正确的行为。
If you want to bring the old behavior back, you can also use this variant that doesn't require a finallyjust for the sake of writing the "if no exceptions" condition:
如果你想恢复旧的行为,你也可以使用这个不需要 a 的变体,finally只是为了编写“如果没有例外”条件:
var checkpointReached = false;
try{
// something
checkpointReached = true;
// i can do what i want here
}catch(Exception e){
if (checkpointReached) throw; // don't handle exceptions after the checkpoint
// handle exception
}
回答by Denys Séguret
You don't need the finally clause.
您不需要 finally 子句。
A solution :
一个办法 :
bool exception = false;
try{
// something
}catch(Exception e){
exception = true;
}
if(!exception){
// u can do what u want here
}
Usually you'll simply have a return in your catch clause so that you don't even have to test :
通常,您只需在 catch 子句中返回一个 return ,这样您甚至不必测试:
try{
// something
}catch(Exception e){
// do things
return;
}
// u can do what u want here
or (depending on the use case and generally less clear, especially if you have more than one exception expected - you don't want to have try-catch nesting...) :
或(取决于用例并且通常不太清楚,特别是如果您预期有多个异常-您不想进行 try-catch 嵌套...):
try{
// something
// u can do what u want here
}catch(Exception e){
// do things
}
回答by LukeH
Nope - what you've got is probably the best way to do it in C#.
不 - 你所拥有的可能是在 C# 中做到这一点的最佳方式。
This is assuming that:
这是假设:
- You don't want the "i can do what i want here"code to run at the bottom of your
tryblock. (Perhaps because you don't want exceptions in that code to be handled by the maincatchblock.) - You don't want the "i can do what i want here"code to run entirely outside of the
try...catch...finallystructure. (Perhaps because you want that code to run before some other code that's sitting inside thefinallyblock.)
- 您不希望“我可以在这里做我想做的事”代码运行在
try块的底部。(也许是因为您不希望主catch块处理该代码中的异常。) - 您不希望“我可以在这里做我想做的”代码完全在
try...catch...finally结构之外运行。(也许是因为您希望该代码在finally块内的其他代码之前运行。)
回答by Jeff Foster
Can you structure your code that the doSomethingis the last statement in the block and it doesn't throw?
您能否将代码结构doSomething化为块中的最后一个语句并且它不会抛出?
bool exception = false;
try{
// something
doSomething();
} catch {
}
finally {
}
回答by Sebastian Breit
Yes there is: put it at the end of the try block :)
是的:把它放在 try 块的末尾:)
回答by James Hill
While there is nothing wrong with your code, it's unnecessary. Simply put the code you wish to execute at the bottom of the try block:
虽然您的代码没有任何问题,但这是不必要的。只需将您希望执行的代码放在 try 块的底部:
try {
...
// No errors to this point, run what you wanted to run in the finally.
}
catch(Exception e) {
...
}
回答by user2202609
I believe you are looking for a try inside your try:
我相信您正在尝试中寻找尝试:
try{
// something
try{
// something else not interfering with first try
} catch(Exception innerEx){
// something else threw this innerEx
}
}catch(Exception outerEx){
// something threw this outerEx
}
Although this is generally considered bad practice, I like it more than the flag version.
虽然这通常被认为是不好的做法,但我比 flag 版本更喜欢它。

