Why does this “finally” execute?

2019-01-23 13:35发布

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?

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-23 14:18

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-23 14:24

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

查看更多
登录 后发表回答