为什么这个“终于”了? 执行?

发布于 2024-07-26 19:09:51 字数 279 浏览 9 评论 0原文

如果运行下面的代码,它实际上会在每次调用 goto 后执行 finally:

    int i = 0;
Found:
    i++;
    try
    {
        throw new Exception();
    }
    catch (Exception)
    {
        goto Found;
    }
    finally
    {
        Console.Write("{0}\t", i);
    }

为什么?

If you run the code below it actually executes the finally after every call to the goto:

    int i = 0;
Found:
    i++;
    try
    {
        throw new Exception();
    }
    catch (Exception)
    {
        goto Found;
    }
    finally
    {
        Console.Write("{0}\t", i);
    }

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鸢与 2024-08-02 19:09:51

以下文本来自C# 语言规范 (8.9.3 goto 语句)


goto 语句的执行方式如下:

  • 如果 goto 语句退出一个或多个带有相关finally 块的try 块,则控制权首先转移到最内层try 语句的finally 块。 当控制到达finally块的结束点时,控制将转移到下一个封闭try语句的finally块。 重复此过程,直到所有介入的 try 语句的finally 块都被执行。
  • 控制权转移到 goto 语句的目标。

The following text comes from the C# Language Specification (8.9.3 The goto statement)


A goto statement is executed as follows:

  • If the goto statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.
  • Control is transferred to the target of the goto statement.

为什么你期望它不执行?

如果您有 try/catch/finally 或 try/finally 块,则无论 try 或 catch 块中包含什么代码,finally 块都会执行大多数时候

考虑“return”而不是“goto”。

//imagine this try/catch/finally block is inside a function with return type of bool. 
try
{
    throw new Exception();
}
catch (Exception)
{
    return false; //Let's say you put a return here, finally block still executes.
}
finally
{
    Console.WriteLine("I am in finally!");
}

Why do you expect it to not execute?

If you have try/catch/finally or try/finally block, finally block executes no matter what code you may have in the try or catch block most of the time.

Instead of goto, consider 'return'.

//imagine this try/catch/finally block is inside a function with return type of bool. 
try
{
    throw new Exception();
}
catch (Exception)
{
    return false; //Let's say you put a return here, finally block still executes.
}
finally
{
    Console.WriteLine("I am in finally!");
}
空‖城人不在 2024-08-02 19:09:51

给出的答案的要点 - 当控制通过任何方式离开受保护区域时,无论是“返回”,“转到”,“中断”,“继续”还是“抛出”,“最终”都会被执行 - 是正确的。 然而,我注意到几乎每个答案都说“finally 块总是运行”。 finally 块并不总是运行。在很多情况下,finally 块不会运行。

谁想尝试将它们全部列出来?

The gist of the answers given - that when control leaves the protected region via any means, whether "return", "goto", "break", "continue" or "throw", the "finally" is executed - is correct. However, I note that almost every answer says something like "the finally block always runs". The finally block does NOT always run. There are many situations in which the finally block does not run.

Who wants to try to list them all?

静待花开 2024-08-02 19:09:51

看起来很合理。 finally 块始终在 trycatch 之后运行。

同样,

try
{
  // do something
  return;
}
finally
{
  // do something else
}

将始终运行 finally 块。 编辑 - 但请参阅上面埃里克的评论。

Seems reasonable. A finally block is always run after either the try or the catch.

Similarly

try
{
  // do something
  return;
}
finally
{
  // do something else
}

will always run the finally block. EDIT - but see Eric's comments above.

可爱暴击 2024-08-02 19:09:51

这是设计使然。 在异常处理程序中,您可以采取一些特定于异常的操作。 在finally 块中,您应该进行资源清理——这就是为什么无论异常处理代码是什么,finally 块总是被执行的原因。

That's by design. In the exception handler you can take some exception-specific action. In the finally block you should do resource cleanup - that's why the finally block is always executed no matter what the exception handling code is.

幸福丶如此 2024-08-02 19:09:51

正如人们所提到的,无论程序流程如何,finally都会运行。 当然,finally 块是可选的,因此如果不需要它,就不要使用它。

As people have mentioned, finally runs no matter the program flow. Of course, the finally block is optional, so if you don't need it, don't use it.

女中豪杰 2024-08-02 19:09:51

因为 finally 语句预计在离开 try(或捕获异常时的 catch)后执行。 这包括当您拨打 goto 电话时。

Because a finally statement is expected to execute after leaving the try (or catch when an exception is caught). This includes when you make your goto call.

像极了他 2024-08-02 19:09:51

这就是finally 块的要点。 它总是执行(几乎)。

That is the point of the finally block. It always executes (pretty much).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文