简体   繁体   English

Try catch finally: 如果没有抛出异常,就做点什么

[英]Try catch finally: Do something if no exception is thrown

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?有没有更好的办法?

Sure there is: put it at the bottom of the try block.当然有:把它放在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 finally just for the sake of writing the "if no exceptions" condition:如果你想恢复旧的行为,你也可以使用这个不需要finally变体,只是为了编写“if no exceptions”条件:

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
}

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
}

Can you structure your code that the doSomething is the last statement in the block and it doesn't throw?你能构造你的代码,使doSomething是块中的最后一个语句并且它不会抛出吗?

bool exception = false;
try{
  // something
  doSomething();
} catch {
}
finally {
}

是的:把它放在 try 块的末尾:)

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 try block.您不希望“我可以在这里做我想做的事”代码运行在try块的底部。 (Perhaps because you don't want exceptions in that code to be handled by the main catch block.) (也许是因为您不希望主catch块处理该代码中的异常。)
  • You don't want the "i can do what i want here" code to run entirely outside of the try...catch...finally structure.您不希望“我可以在这里做我想做的”代码完全在try...catch...finally结构之外运行。 (Perhaps because you want that code to run before some other code that's sitting inside the finally block.) (也许是因为您希望该代码在位于finally块内的其他一些代码之前运行。)

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) {
    ...
}

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 版本更喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM