7

I am aware of how unhanded exceptions are processed when using Tasks, only throwing an unhandled in the finalizer if user code hasn't 'observed' it yet.

I am also aware of how an unhandled exception in an async thread (e.g. Action.BeginInvoke()) is caught and re-thrown on the joining call (e.g. Action.EndInvoke()).

What I don't understand though is how this doesn't crash the process?

    static void Main(string[] args)
    {
        var timer = new System.Timers.Timer() {Interval = 100};
        timer.Elapsed += (o, e) => { throw new Exception(); };
        timer.Start();

        Console.ReadKey( true );
    }
Tyson
  • 14,726
  • 6
  • 31
  • 43
  • Well this was 4-6 hours of my life i'll never get back. What a horrible decision on Microsoft's part for this to not crash. – Chris Marisic Aug 10 '15 at 14:33

1 Answers1

9

From the .NET 4.0 documentation:

In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

There is no statement yet claiming that this behavior has actually changed.

Polity
  • 14,734
  • 2
  • 40
  • 40
  • Ahh that would be it, Thanks - I did actually read that page, but must of missed it. I think I foolishly assumed that something that goes against all of their standard practice of not suppressing unhandled exceptions would have been inside one of those nice yellow boxes. – Tyson Feb 23 '12 at 04:26
  • @Tyson, agreed. It introduces an ugly inconsistence with other places. – Lex Li Feb 23 '12 at 06:11