简体   繁体   中英

Exception thrown in Task Thread, not caught by UnobservedTaskException

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM