【发布时间】:2012-04-25 23:39:51
【问题描述】:
我正在使用任务并行库来运行一个任务,该任务在取消时会引发 OperationCanceledException,然后使用 AggregateException 捕获该异常,如下所示。 AggregateException 包含一个 TaskCanceledExceptions 列表,它们对应于抛出的异常。不幸的是,这些 TaskCanceledExceptions 似乎正在丢失原始异常引发的堆栈跟踪。这是设计的吗?
try
{
task1.Wait();
}
catch (AggregateException aggEx)
{
var tcex = ex as TaskCanceledException;
if (tcex != null)
{
Debug.WriteLine("InnerException:{0}, Message:{1}, Source:{2}, StackTrace: {3}",
tcex.InnerException, tcex.Message, tcex.Source, tcex.StackTrace);
return true;
}
else
{
return false;
}
}
结果:
InnerException:, Message:A task was canceled., Source:, StackTrace:
【问题讨论】:
-
请更新您的代码以反映您发布的结果! :)
-
“if (tcex != null)”行是否正确?似乎应该是“if (tcex == null)”
-
@MatthewWatson - 这个
if过滤掉其他异常。 -
我已更新问题以反映您的 cmets。问题是一旦抛出异常,堆栈跟踪在到达 AggregateException 捕获时丢失
标签: c# .net-4.0 task-parallel-library cancellation aggregateexception