【发布时间】:2016-06-01 09:18:16
【问题描述】:
我正在使用Task 加载数据。如果失败,我想向用户展示失败的根本原因。
在Task 执行期间发生的异常被聚合到AggregatedException。那么,从中获取导致异常的根源的最佳方法是什么?
我目前的做法是:
public static Exception GetRootCausingException(this AggregateException aggregatedException)
{
if (aggregatedException == null)
return null;
// get the AggregateException that is the root cause of this exception.
var exception = aggregatedException.GetBaseException();
while (exception.InnerException != null) exception = exception.InnerException;
return exception;
}
【问题讨论】:
-
最里面的异常通常是没有意义的。外层经常添加或解释信息。您可能应该去掉 aggex,然后显示所有异常(例如 ToString)。
标签: c# exception-handling task-parallel-library task