Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 770
bot_insane Avatar asked Sep 29 '15 07:09

bot_insane


2 Answers

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[])
like image 137
Salah Akbari Avatar answered Oct 21 '22 07:10

Salah Akbari


Werfault.exe (Windows Error Reporting) is called for software targeting Framework 4 or greater.
It does behave as you mention.
Dw20.exe (Dr Watson) is called for Framework 3.5 or below.
It behaves correctly (always call finally).

The behaviour of these (non .net) programs are different. It may comes from the way Werfault produces its minidump (kill the program before finally) which is obviously buggy.

As some users have already noticed, WerFault could be somewhat a "faulty" program.

Anyway, for a developper, I don't think sending data to Microsoft about your "Console 9" application is relevant. So you can safely disable the error reporting :

set HKLM/SOFTWARE/Microsoft/Windows/Windows Error Reporting/Disabled to 1

As a conclusion, if you have some concern about privacy you wouldn't let Werfault send any data to Microsoft as NSA could use it.

like image 28
Fab Avatar answered Oct 21 '22 06:10

Fab