From the Microsoft documentation the System.Timers.Timer elapsed method should swallow all exceptions.
The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event.
https://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
However when subscribing using an async void method an exception is produced which crashes the application. See the below code:
class Program { static void Main(string[] args) { Timer timer = new Timer(100); //timer.Elapsed += On_ElapsedSync; //A //timer.Elapsed += On_ElapsedAsync; //B timer.Elapsed += On_ElapsedAsyncVoid; //C timer.Start(); Console.WriteLine("Running..."); Console.ReadLine(); } private static void On_ElapsedSync(object sender, ElapsedEventArgs e) { Console.WriteLine("Throwing..."); throw new Exception("My Exception"); } private static void On_ElapsedAsync(object sender, ElapsedEventArgs e) { Console.WriteLine("Throwing..."); Task.Run(() => throw new Exception("Async Exception")); } private static async void On_ElapsedAsyncVoid(object sender, ElapsedEventArgs e) { Console.WriteLine("Throwing..."); await Task.Run(() => throw new Exception("Async Exception")); } } The lines commented A and B do not crash the application. The line commented C does.
Why is this the case?
async voidexceptions cannot be caught by normal means.