目前我正在尝试随.Net Framework 4.0 Beta 2一起提供的任务并行库的一些新功能.
我的问题特别涉及TPL中的异常处理,如下所述:http: //msdn.microsoft.com/en-us/library/dd997415%28VS.100%29.aspx
第一个例子(改变了一点):
static void Main(string[] args)
{
var task1 = Task.Factory.StartNew(() =>
{
throw new Exception("I'm bad, but not too bad!"); // Unhandled Exception here...
});
try
{
task1.Wait(); // Exception is not handled here....
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions)
{
Console.WriteLine(e.Message);
}
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
根据文档,Exception应该传播回到调用的连接线程:task1.Wait().
但我总是得到一个未处理的例外:
var task1 = Task.Factory.StartNew(() =>
{
throw new MyCustomException("I'm bad, but not too bad!");
});
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么,或者有人知道自Beta 2发布以来是否有某些变化?