【发布时间】:2011-11-12 12:13:04
【问题描述】:
static async void Main(string[] args)
{
Task t = new Task(() => { throw new Exception(); });
try
{
t.Start();
t.Wait();
}
catch (AggregateException e)
{
// When waiting on the task, an AggregateException is thrown.
}
try
{
t.Start();
await t;
}
catch (Exception e)
{
// When awating on the task, the exception itself is thrown.
// in this case a regular Exception.
}
}
在 TPL 中,当在 Task 中抛出异常时,它会被 AggregateException 包裹。
但使用 await 关键字时,情况并非如此。
这种行为的解释是什么?
【问题讨论】:
-
..这是
await的好处之一
标签: c# exception-handling task-parallel-library async-await parallel-extensions