Task.Exception 属性


获取导致 AggregateException 提前结束的 TaskGets the AggregateException that caused the Task to end prematurely. 如果 Task 成功完成或尚未引发任何异常,这将返回 nullIf the Task completed successfully or has not yet thrown any exceptions, this will return null.

 property AggregateException ^ Exception { AggregateException ^ get(); };
public AggregateException Exception { get; }
public AggregateException? Exception { get; }
member this.Exception : AggregateException
Public ReadOnly Property Exception As AggregateException



导致 AggregateException 提前结束的 TaskThe AggregateException that caused the Task to end prematurely.


引发未经处理的异常的任务存储生成的异常,并将其传播 AggregateException 到中的调用 Wait 或访问 Exception 属性的。Tasks that throw unhandled exceptions store the resulting exception and propagate it wrapped in a AggregateException in calls to Wait or in accesses to the Exception property. 在 .NET Framework 4.0 上,在对任务实例进行垃圾回收时未观察到的任何异常都将在终结器线程上传播,这会导致进程崩溃。On .NET Framework 4.0, any exceptions not observed by the time the task instance is garbage collected will be propagated on the finalizer thread, which crashes the process. 在 .NET Framework 4.5 和更高版本中,默认行为已更改,因此不会从终结器重新引发未观察到异常。On .NET Framework 4.5 and later the default behavior changed so unobserved exceptions are not rethrown from the Finalizer. .NET Core 不会重新引发终结器中的异常。.NET Core does not rethrow the exception on the Finalizer. 有关详细信息和示例,请参阅 异常处理 (任务并行库) For more information and an example, see Exception Handling (Task Parallel Library).