locked
TaskScheduler.UnobservedTaskException not called ? RRS feed

  • Question

  • User2029 posted

    Hi guys, i have this code in a PCL class

            Task.Run(() =>
            {
                  throw new Exception("bug");
            }
    

    which is run from an UIView ViewDidLoad.

    I also have a global handler for exception that arises in tasks (set in AppDelegate.FinishedLaunching):

            TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
    

    and

        private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
        {
            Console.WriteLine("task exception: {0}", unobservedTaskExceptionEventArgs.Exception);
        }
    

    The problem is: the handler is never called when the exception is in a task started from the PCL (portable class lib).

    I suppose that the TaskScheduler in AppDelegate is not the same as the one in the PCL. But it should be no ?

    Tuesday, March 11, 2014 6:46 AM

All replies

  • User1683 posted

    Are you also listening to AppDomain.CurrentDomain.UnhandledException?

    That looks like it will run and throw synchronously... what happens if you do this:

        Task.Run(async () =>
                 {
                     await Task.Delay(100);
                     throw new Exception();
                 {);
        GC.Collect();
        GC.WaitForPendingFinalizers();
    
    Thursday, March 13, 2014 5:36 AM
  • User2029 posted

    Yes i already have both. The behavior is, visual studio just stops debugging. And when not debugging nothing is written in the console window. If i add a try catch around the crashing code inside the task, the catch is correctly called though.

            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                Console.WriteLine("Exception from " + s.GetType());
                Console.WriteLine(e.ToString());
            };
    
            TaskScheduler.UnobservedTaskException += (s, e) =>
            {
                Console.WriteLine("Task exception");
                Console.WriteLine(e.Exception.ToString());
            };
    
    Thursday, March 13, 2014 8:00 AM
  • User2029 posted

    Hi, this now works on iOS.

    BUT it still does not work on Android. 2 years later ...

    Tuesday, December 13, 2016 11:53 AM