When I suspect a Task‘s Action had failed I used to just sit waiting for the AggregateExceptionto finally get thrown so I could dig into the InnerException. Recently, however, I noticed that by attempting to resize the form which had initiated the failing Task I could force the error to throw visibly.
Two questions:
- Is there a better way to do this? (I hate the “Wrap the call in an try/catch/re-
throwand put a break point on the re-throwtechnique). - I thought the
AggregateExceptiongot thrown when theTaskobject itself is garbage collected. Is that true? - In light of the answer to #2: Why does resizing trigger the
AggregateExceptionmore immediately?
You can either use the
Resultproperty or theWait()function. Both block and if theTaskfails, they throw theAggregateException. Another option, if you want to do something with the exception, is to useContinueWith()withTaskContinuationOptions.OnlyOnFaulted.Yes, if the
Exceptionof theTaskis not accessed in any way, it will throw the exception when it’s finalized on .Net 4.0. (.Net 4.5 changes that, unhandled exceptions fromTasks no longer throw when theTaskis finalized.)Most likely because it creates some objects, which causes the garbage collection to run.