Unhandled Exceptions and werfault
Asked Answered
C

3

7

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.

Clank answered 29/9, 2015 at 7:42 Comment(2)
This could be framework version specific. Which version do you use?Rockbottom
@ThomasWeller See updated question please:Clank
C
1

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[])
Champlain answered 1/10, 2015 at 20:2 Comment(1)
Thank you for answer. Is there any documentation that WerFault.exe should terminate program immediately?Clank
O
1

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.

Operate answered 2/10, 2015 at 4:29 Comment(0)
R
-1

try this:

 static void Main(string[] args)
    {
        System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
        try
        {
            throw new Exception("Exception");
        }
        catch
        {
            throw;
        }
        finally
        {
            Console.WriteLine("------finally-----");
        }
    }

    static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine(e.ExceptionObject.ToString());
        Console.WriteLine("Press Enter to continue");
        Console.ReadLine();
        Environment.Exit(1);
    }

i find it on:.NET Global exception handler in console application

Riggins answered 29/9, 2015 at 7:48 Comment(2)
Thank you for answer, but my question isn't to handle unhandled exceptions, I want to know why this is acting like this ?Clank
You don't check for IsTerminating. And Console.Readline means interaction which is not needed if you're not in the debugger but running from command lineOperate

© 2022 - 2024 — McMap. All rights reserved.