Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why an unhandled exception on timer thread doesn't crash the process

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 );
    }
like image 501
Tyson Avatar asked Feb 23 '12 04:02

Tyson


1 Answers

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.

like image 156
Polity Avatar answered Oct 14 '22 21:10

Polity