I am probably missing a very obvious fact, but I have a hard time understanding the need for AggregatedExceptions.
I know since async/await we don't have to bother with AggregatedExceptions anymore (or at least are confronted with them less frequently). I can relate to that, because I simply start a task and at some time I choose to synchronize the 'calling thread' with the 'task which runs in parallel'
var sometTask = DoSomethingInParallelAsync().ConfigureAwait(false);
...
...
await someTask;
In this scenario, an exception that occurred in DoSomethingInParallelAsync()
will be presented to me when I await that call.
Why is that so different without using await
, but Wait
on the Task
explicitly?
var someTask = DoSomethingInParallelAsync();
...
...
someTask.Wait();
An exception thrown in this example, will always wrap the thrown Exception
in an AggregateException
. I still don't understand why.
What can't I do when using async/await which takes away the need for AggregateException
s?
Causing and handling the error
Task task1 = Task.Factory.StartNew(() => { throw new ArgumentException(); } );
Task task2 = Task.Factory.StartNew(() => { throw new UnauthorizedAccessException(); } );
try
{
Task.WaitAll(task1, task2);
}
catch (AggregateException ae)
{
}
and also see that links
here is an example: link2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.