Exception thrown in Task Thread, not caught by Uno

2019-07-01 21:26发布

问题:

I'm having trouble understanding how Exceptions are handled in TPL. The following code should illustrate my problem.

using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace WebDLApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; // Not catching exception

            List<string> sites = new List<string>{ "http://microsoft.com", "http://yahoo.com", "http://facebook.com", "http://amazon.com", "http://foooo", "http://aol.com", "http://ask.com", "http://wikipedia.org" };
            List<Task<string>> tasks = new List<Task<string>>();


            foreach (string site in sites)
            {
                tasks.Add(Task.Factory.StartNew<string>((wsite) =>
                    {
                        using (WebClient wc = new WebClient())
                        {
                            wc.DownloadString((string)wsite); // Thrown here, always
                            return (string)wsite;
                        }
                    }, site)
                );
            }


            Task.WaitAll(tasks.ToArray()); // Can't catch here

            int counter = 1;
            foreach (var task in tasks)
            {

                Console.WriteLine(counter.ToString() + task.Result); // Can't catch here either
                counter++;
            }

            Console.ReadLine();
        }

        static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) // Never called
        {
            Console.WriteLine(e.Exception.Message);
            e.SetObserved();
        }
    }
}

From what I understand, using the TaskScheduler.UnobservedTaskException event object is a pretty good way to help handle exceptions from tricky third-party libraries. However, the Exception seems to be always thrown within the Task. It looks as if it never bubbles up to be caught by the TaskScheduler (or whatever facility handles TaskExceptions).

For code brevity, I've omitted possible try/catch locations and marked them with a comment.

I'm expecting the TaskScheduler_UnobservedTaskException event handler to print to the console and observe the Exception. However, once the execution reaches the Result of the task, a WebException is thrown from within the Task.

回答1:

Here (How to handle exceptions with TaskScheduler.UnobservedTaskException?) is the explanation. Just replace

Task.WaitAll(tasks.ToArray()); // Can't catch here

with

Task.Factory.StartNew(() =>
{
    while (true)
    {
        Thread.Sleep(1000);
        GC.Collect();
    }
});
return;

to see the message in TaskScheduler_UnobservedTaskException