I have an async method in a .NET 4.5 C# component:
public async Task<T> GetResultAsync()
{
return PerformOperationAsync();
}
If PerformOperationAsync throws an exception, then I can catch an AggregateException on a client side, unwrap it and get the original exception thrown.
However, if I have slightly more complicated code:
public async Task<T> GetResultAsync()
{
return PerformOperationAsync().ContinueWith(x =>
{
var result = x.Result;
return DoSomethingWithResult(result);
}, cancellationToken);
}
... then in case an exception occurs, the client catches a nested AggregateException, so it has to flatten it prior getting the original one.
Should this behavior be avoided or does client have to expect possibly nested AggregateException and call Flatten to unwrap all its levels? And if this behavior should be avoided by the component developer, then what's the right way to deal with it in ContinueWith scenario? I have plenty of similar situations, so I am trying to find the most lightweight method of handling them.
awaitinstead.IOException, or someMySpecificException? For example, Microsoft Orleans considers each indirection a separate scope for aggregate exception, so each asynchronous call is a new layer in the resulting aggregate exception. Does that make sense for your case? It really is just basic system / API architecture, it's not specific to tasks or aggregate exceptions :)