TaskCanceledException in AggregateException does not contain stack trace

I use a parallel task library to run a task that, if canceled, throws an OperationCanceledException, which is then thrown using the AggregateException as follows. The AggregateException tool contains a list of TaskCanceledExceptions that correspond to thrown exceptions. Unfortunately, these TaskCanceledExceptions seem to lose stack traces caused by the original exceptions. Is it for 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: 
+6
source share
1 answer

I would think that by design, canceled tasks are excluded, usually only thrown when the task is canceled as an average to help complete the task. What is the purpose of stack tracing in this situation? This suggests that I do not have an understanding of the thinking process of the developer who developed the exceptions for the exception, just my opinion.

I'm not quite sure what you mean, do they lose the stack trace coming from the original exception? Usually they would not have an internal / original exception. Perhaps you cancel the task due to another exception? In this case, you should catch the β€œoriginal” exception up, register it there, and then undo after.

+1
source

Source: https://habr.com/ru/post/913138/


All Articles