UnobservedTaskException. What am I missing? Why is the event not fireing?

Thats the mockup code:

Task t3 will have ample time to finish and throw an Exception in the background. Since it is not waited with t3.Wait() its exception is unhandled and should be collected to be signalled by UnobservedTaskException event.

using System; using System.Threading.Tasks; using System.Threading; class Program { static void ThrowEx() { throw null; } static void Main(string[] args) { TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs> (TaskScheduler_UnobservedTaskException); Task t3 = Task.Run(() => { throw new Exception("t3 throw an exception"); }); Thread.Sleep(5000); } static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { Console.WriteLine( "Error" ); Console.WriteLine(e.Exception.Message); e.SetObserved(); } } 

submitted by /u/Fullduplex1000
[link] [comments]

Leave a Reply