Bug in CLR? CLR execution engine failed

3

AFAIK, try and finally block are used execute a piece of code that might throw some exception, we also add catch block if we are prepared to handle some type of exception and/or are excepting them, like FileIOException, AccessRight or something. But when I ran this..

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            Environment.FailFast("It failed");
        }
        finally
        {
            MessageBox.Show("Done");
        }
    }

Its breaks with an exception and says

FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was at 0x032526f4, on thread 0xd04. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Now msdn says

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.

So, I added the catch block, but still it says the same thing.

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            Environment.FailFast("It failed");
        }
        catch (Exception ex)
        {

        }
        finally
        {
            MessageBox.Show("Done");
        }
    }

It failed again with the same error. As for CLR saying the code block in finally always runs (at least when catch is added), it surely is not the case. Reviews/Opinions anyone?

Also here's the snapshot..

enter image description here

c#
c#-4.0
clr
asked on Stack Overflow Mar 2, 2013 by Razort4x • edited Oct 26, 2015 by pnuts

1 Answer

7

This is by design. The purpose of Environment.FailFast is to halt execution immediately. By design it will not run any code in catch or finally blocks.

The documentation says:

This method terminates a process without running any active try/finally blocks or finalizers.

The FailFast method writes the message string to the Windows Application event log, creates a dump of your application, and then terminates the current process. The messa string is also included in error reporting to Microsoft.

Use the FailFast method instead of the Exit method to terminate your application if the state of your application is damaged beyond repair, and executing your application's try/finally blocks and finalizers will corrupt program resources.

This makes it clear that code in your finally blocks will not run. If there was a way to make code run after Environment.FailFast then that would render Environment.FailFast pretty much useless. Its very existence is predicated on the fact that your code does not execute after you call it.

You point to documentation that states (emphasis mine):

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement.

But those words simply do not apply here. You are assuming that when you call Environment.FailFast, an unhandled exception terminates the application. That is not the case. The application is just terminated on the spot – there is no unhandled exception.

answered on Stack Overflow Mar 2, 2013 by David Heffernan • edited Mar 2, 2013 by David Heffernan

User contributions licensed under CC BY-SA 3.0