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关键字
时不会发生同样的情况。
这种行为的解释是什么?