Async and AggregateException


Async and AggregateException

this post is a complementary to Eran Stiller’s post.

I was reading Eran Stiller’s post about exception handling using async methods and I want to add a few side notes.

async, await, parallel, task, tpl, tap,.net4.5,c#5, exception

1) the exception handling behavior decisions is well documented in this post (by the TPL team), it decided after they had consider different options.

2) I was suggesting that compiler will check whether the a catch of AggregateException is implemented and if so to avoid the unwrapping behavior.
in this case the it can be assumed that programmer do expect multiple exceptions.
unfortunately, for the current version, I was  speaking with them after the release of .NET 4.5.
I hope that we will see this mitigation in future release.

3) for the current version you can use the following work around if you do want to catch aggregate exception:

Code Snippet
  1. await Task.WhenAll(t1, t2)
  2.     .ContinueWith(t => { if (t.Exception != null) throw t.Exception; });

by re-throwing the exception the unwrapped exception will be AggregateException.
it may be wise to wrap this pattern in an extension method because it can be a very repetitive one.

Proper Disclosure

Eran is one of my colleague and definitely one of the more talent Architect I was happened to work with.

Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>