Conditions when finally does not execute in a .net try..finally block

.NetException Handling

.Net Problem Overview


Basically I've heard that certain conditions will cause .net to blow past the finally block. Does anyone know what those conditions are?

.Net Solutions


Solution 1 - .Net

Two possibilities:

The finally block will not be executed when there's a StackOverflowException since there's no room on the stack to even execute any more code. It will also not be called when there's an ExecutionEngineException, which may arise from a call to Environment.FailFast().

Solution 2 - .Net

Unless the CLR blows up and goes down with an ExecutingEngineException (I've seen a few in the .net 1.1 days with just the right amount of COM Interop :) .. I think finally should always execute.

Solution 3 - .Net

You can get a situation where the code in the try block causes a SecurityException to be thrown before the try block entered (instead the exception is thrown when the containing method is called (see http://msdn.microsoft.com/en-us/library/fk6t46tz(VS.71).aspx)), in this situation you never even enter the try block so the code in the finally block is never called.

Other possibilities include StackOverflowException, and ExecutingEngineException.

Solution 4 - .Net

Finally block on background thread may not execute. However, It depends upon the completed execution of main foreground thread which terminates background thread operation even before the complete execution of background thread.

class Program
{

    static void Main(string[] args)
    {
        Program prgm = new Program();
        Thread backgroundThread = new Thread(prgm.CheckBgThread);
        backgroundThread.IsBackground = true;
        backgroundThread.Start();
        Console.WriteLine("Closing the program....");
    }

    void CheckBgThread()
    {
        try
        {
            Console.WriteLine("Doing some work...");
            Thread.Sleep(500);
        }
        finally
        {
            Console.WriteLine("This should be always executed");
        }
    }
}

Solution 5 - .Net

There is also Application.Exit method.

Solution 6 - .Net

Since async/await, there is another way a finally might get ignored that I haven't seen mentioned in other answers:

static class Program
{
    [STAThread]
    static void Main()
    {
        async void ThreadExecutionAsync()
        {
            try
            {
                SynchronizationContext.SetSynchronizationContext(
                    new WindowsFormsSynchronizationContext());

                await Task.Yield(); // Yield to the context

                // The WindowsFormsSynchronizationContext will schedule the continuation
                // on the main thread, so the current thread will die
                // and we will never get here...
                Debugger.Break();
            }
            finally
            {
                // Will never get here either...
                Debugger.Break();
            }
        }

        var thread = new Thread(ThreadExecutionAsync);
        thread.Start();

        Application.Run();
    }
}

Solution 7 - .Net

Neither code which follows a finally block, nor code in outer scopes, will execute without the finally block having been started first (an exception within the finally block may cause it to exit prematurely, in which case execution will jump out from the finalizer to an outer scope). If code prior to the finally block gets stuck in an endless loop or a method that never exits, or if the execution context is destroyed altogether, the finally block will not execute.

Note that it is proper to rely upon finally blocks, unlike "Finalize" methods (or C# "destructors") which should not properly be relied upon.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNotMeView Question on Stackoverflow
Solution 1 - .NetHaackedView Answer on Stackoverflow
Solution 2 - .NetGishuView Answer on Stackoverflow
Solution 3 - .NetChrisView Answer on Stackoverflow
Solution 4 - .NetNavneetView Answer on Stackoverflow
Solution 5 - .NetNicuView Answer on Stackoverflow
Solution 6 - .NetMelvynView Answer on Stackoverflow
Solution 7 - .NetsupercatView Answer on Stackoverflow