我无法理解如何在TPL中处理异常。下面的代码应该说明我的问题。在任务线程中抛出的异常,未被UnobservedTaskException捕获
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();
}
}
}
据我所知,使用TaskScheduler.UnobservedTaskException事件目的是帮助处理来自棘手的第三方库例外一个很好的方式。但是,异常似乎总是在任务中抛出。它看起来好像永远不会被TaskScheduler(或任何设备处理TaskExceptions)所捕获。
为简洁起见,我省略了可能的try/catch位置并用注释标记了它们。
我期待TaskScheduler_UnobservedTaskException事件处理程序打印到控制台并观察异常。但是,一旦执行达到任务的结果,WebException将从Task中抛出。
你还没说你实际上看到的行为。我猜你的'Task.WaitAll'调用正在抛出'AggregateException'。如果是这样,那将是预期的行为:你没有任何未被察觉的例外。 'Wait' /'WaitAll' /'WaitAny'会传播你正在等待的任务中的任何异常,此时它不再被观察到。 –