I am using the Task Parallel Library to run a task which - when cancelled - throws the OperationCanceledException, which is then caught using the AggregateException, as follows. The AggregateException contains a list of TaskCanceledExceptions, which correspond to the thrown exceptions. Unfortunately, these TaskCanceledExceptions seem to be losing the stack traces thrown by the original exceptions. Is this by design?
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;
}
}
Result:
InnerException:, Message:A task was canceled., Source:, StackTrace: