18

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?

Grace Note
  • 3,205
  • 4
  • 35
  • 55
Kredns
  • 36,461
  • 52
  • 152
  • 203

8 Answers8

36

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.
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
28

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!");
}
Community
  • 1
  • 1
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
14

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?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • According to the spec "The statements of a finally block are always executed when control leaves a try statement. This is true whether the control transfer occurs as a result of normal execution, as a result of executing a break, continue, goto, or return statement, or as a result of propagating an exception out of the try statement." However, I'd say a StackOverflowException is one, because nothing can deal with it as far as I can see. – Colin Mackay Jul 17 '09 at 07:29
  • 2
    Colin, indeed, the key point is WHEN CONTROL LEAVES. If control doesn't leave then the finally block does not execute! The try-protected region might contain an infinite loop, for example. – Eric Lippert Jul 17 '09 at 14:02
  • @Eric: I totally got this question from your blog post. I also didn't think about an infinite loop in a try/catch block – Kredns Jul 17 '09 at 14:57
  • 1
    Environment.FailFast(); and OutOfMemoryException will both avoid running the finallly block. – Neil May 12 '10 at 20:29
  • @neilwhitaker1: When one of those things happens, does control really leave the Finally block, or does control just plain die? How about saying "control won't go anywhere outside the finally block without executing it first, absent the OS killing or taking over the thread and tossing the concept of "control flow" out the window? – supercat Jul 28 '10 at 17:42
3

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.

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
2

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

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.

Merus
  • 8,796
  • 5
  • 28
  • 41
0

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.

statenjason
  • 5,140
  • 1
  • 32
  • 36
0

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

fastcodejava
  • 39,895
  • 28
  • 133
  • 186