24

Why isn't the line "Console.WriteLine("asdf");" executed? All the others are. Shouldn't it also be as we can't jump out from the finally scope?

static bool Func()
{
    try
    {
        try
        {
        }
        finally
        {
            try
            {
                throw new ApplicationException();
            }
            finally
            {
                Console.WriteLine("asd");
            }

            Console.WriteLine("asdf");
        }
    }
    finally
    {
        Console.WriteLine("asd");
    }
}
Kocsis Dávid
  • 529
  • 1
  • 3
  • 14
  • 3
    Related: [What Happens In C# If A Finally Block Throws An Exception?](http://stackoverflow.com/questions/2911215) – H H Jun 14 '12 at 19:21

7 Answers7

30

Finally blocks only guarantee (at least mostly guarantee, see excerpt from MSDN below) that they will be entered in the event that the try block throws an exception. If you throw an exception within the finally block, the exception will cause control to leave the finally block and the rest of the code within that finally block will not execute.

In your case, the line that isn't being executed is occurring after an exception in the same finally block, so it gets skipped.

From MSDN - try-finally:

The finally block is useful for cleaning up any resources that are allocated in the try block, and for running any code that must execute even if an exception occurs in the try block. Typically, the statements of a finally block are executed when control leaves a try statement, whether the transfer of control occurs as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.

Note: Unhandled Exception Processing in the CLR is a reference to an article in the September 2008 issue of the MSDN Magazine. All 2008 and older issues of MSDN Magazine are only available as .chm files, and will need to be downloaded before viewing.

Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
  • `"Finally blocks only guarantee *snip* that they will be entered if the try block throws an exception"` you mean finally blocks only enter after a try block exits? (catch blocks execute on an exception) – James Khoury Jun 15 '12 at 07:01
  • 1
    That is true. I was trying to point out how they handle exceptions, and the fact that they don't guarantee that they will be run in their entirety, but that they will at least be entered. – Jon Senchyna Jun 15 '12 at 12:39
  • See article [MSDN Magazine 2008 September: Unhandled Exception Processing in the CLR](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazine2008_09en-us.chm). (to open chm it need to unlock: File Properties -> General -> Unlock) – vladimir Aug 30 '17 at 20:21
19

I think the best way this could be answered is by using the code and hence the following image enter image description here

P.K
  • 18,587
  • 11
  • 45
  • 51
6

Because the exception is being thrown in that finally block, so it causes control to fall out to the final finally block. So the "asdf" WriteLine never executes.

dcp
  • 54,410
  • 22
  • 144
  • 164
4

Exceptions thrown within a finally(or catch) block cancel the remainder of that finally(or catch) block.

3

The error occurs inside the 3rd try block which causes its corresponding finally to be executed. However, this causes it to error out of the current finally and be caught by the original try-finally block.

barbiepylon
  • 891
  • 7
  • 23
1

because you have throw in try block and it will execute finally block with Console.WriteLine("asd"); and exit to outer try catch

Damith
  • 62,401
  • 13
  • 102
  • 153
-1

When I used try-finally blocks on code deployed across different host platorms I found that: when no exceptions were thrown on the try block.

some Windows platforms always executed the finally block and some platforms never executed the finally block.

My finally block contained instructions to close down the error log and failure to execute the finally block always raised another exception on exit leaving only cryptric innformation available to find out what caused the error. For my application try finally blocks were more trouble than they were worth.

anc
  • 1