Search code examples
c#exceptionwindows-error-reporting

Unhandled Exceptions and werfault


Consider we have this snippet of code:

static void Main(string[] args)
{
    try
    {
        throw new Exception("Exception");
    }
    catch
    {
        throw;
    }
    finally
    {
        Console.WriteLine("------finally-----");
    }
}

We have unhandled exception and finally block.

When werfault is enabled and I press Cancel when it is trying to "automatically solve problem" finally block executes.

But if I'm not pressing Cancel and in the next window clicking Close The Program finally block doesn't execute.

And finally when I disable werfault and click Close Program finally block executes.

I didn't find any documentation in c# spec that describes this situation. Also I found this on MSDN:

Execution of the finally block after an unhandled error depends on how the exception unwind operation is triggered.

But there is no explanation, anyone can describe why this is happening ?

Update 1: I have tested it in .Net Framework 4.5.1 and 4.5.2.


Solution

  • WerFault.exe terminates and kills the .NET process immediately due to an unhandled exception but when you disable WerFault Microsoft .NET Error Reporting somehow closes the application so that the finally block still is executed and does not terminate the process.

    The best way to check that WerFault.exe terminates process immediately is the Event Viewer (Description: The process was terminated due to an unhandled exception.).

    Application: ConsoleApplication3.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.Exception
    Stack:
       at ConsoleApplication3.Program.Main(System.String[])