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:
I would think it's by design, task canceled exceptions are normally only thrown when a task is canceled as mean to help the task to exit. What purpose does a stack trace have in this situation? That being said I don't have insight into the thought process of the dev's that designed the cancellation exceptions, just my opinion.
I'm not quite sure what you mean by they lose the stack trace thrown by the original exception? Normally they would not have an inner/original exception. Perhaps you are canceling a task due to some other exception? In that case you should be catching the "original" exception upstream, logging it there, then canceling after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With