Why does this “finally” execute?

前端 未结 8 887
故里飘歌
故里飘歌 2020-12-14 19:02

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 Exc         


        
相关标签:
8条回答
  • 2020-12-14 19:31

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

    0 讨论(0)
  • 2020-12-14 19:33

    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.
    0 讨论(0)
提交回复
热议问题