Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do you handle AggregateException? It is really useful in many TPL or business scenarios, but handling is a real pain. I defined the following three extensions to help myself a little bit. They can be used this way:

try
{
  throw new AggregateException(
     new ArgumentException(), 
     new DivideByZeroException());
}
catch (AggregateException aex)
{   
  aex
     .Handle((ArgumentException ex) =>
     {
         Console.WriteLine(ex.Message);
     })
     .Handle((DivideByZeroException ex) =>
     {
         Console.WriteLine(ex.Message);
         throw ex;
     })
     .Throw<DivideByZeroException>()
     .Throw<ArgumentException>()
     .ThrowIfNonEmpty();
}

These methods are defined on AggregateException and return AggregateException so they can be chained.

Handle invokes parameter delegate once per each inner exception instance of a specified type and returns a new copy of AggregateException without those handled exceptions. An optional exception being thrown in the delegate will be added to the resulting AggregateException.

Throw method tries to find a first inner exception instance of specified type and throws it if found. You can chain them to decide on priority.

ThrowIfNonEmpty extends an AggregateException to throw it when InnerExceptions collection is not empty.

public static class AggregateExceptionExtensions
{
    public static AggregateException Handle<TEx>(
        this AggregateException source, Action<TEx> handler)
        where TEx : Exception
    {
        var exceptions = source.Flatten().InnerExceptions.OfType<TEx>();
        var handlerExceptions = new List<Exception>();
        foreach (var exception in exceptions)
            try
            {
                handler(exception);
            }
            catch (Exception ex)
            {
                handlerExceptions.Add(ex);
            }

        return new AggregateException(source.InnerExceptions
            .Except(exceptions)
            .Concat(handlerExceptions));
    }

    public static AggregateException Throw<TEx>(this AggregateException source)
    {
        var ex = source.InnerExceptions.FirstOrDefault(e => e is TEx);
        if (ex != null)
            throw ex;

        return source;
    }

    public static void ThrowIfNonEmpty(this AggregateException source)
    {
        if (source.InnerExceptions.Any())
            throw source;
    }
}

Can you suggest any other tricks which might be useful?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.