Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AggregateException C# example

I have seen an example of AggregateException on the web and I'm trying to figure out how it works. I have written a simple example, but my code for some reason doesn't work.

Could someone explain to me what the problem is?

public static void Main()
{
    try
    {
        Parallel.For(0, 500000, i =>
        {
            if (i == 10523)
                throw new TimeoutException("i = 10523");
            Console.WriteLine(i + "\n");
        });
    }
    catch (AggregateException exception)
    {
        foreach (Exception ex in exception.InnerExceptions)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}
like image 302
Dan Avatar asked Jul 19 '11 23:07

Dan


People also ask

What is an AggregateException C#?

AggregateException is used to consolidate multiple failures into a single, throwable exception object. It is used extensively in the Task Parallel Library (TPL) and Parallel LINQ (PLINQ). For more information, see Exception Handling and How to: Handle Exceptions in a PLINQ Query.

How do I fix AggregateException occurred?

Turn on Break on All Exceptions (Debug, Exceptions) and rerun the program. This will show you the original exception when it was thrown in the first place. (comment appended): In VS2015 (or above). Select Debug > Options > Debugging > General and unselect the "Enable Just My Code" option.

What does AggregateException flatten do?

You can also use the AggregateException. Flatten method to rethrow the inner exceptions from multiple AggregateException instances thrown by multiple tasks in a single AggregateException instance, as the following example shows. C# Copy. using System; using System.

Does await throw AggregateException?

When using await, it's going to unwrap the first exception and return it, that's why we don't hit the catch (AggregateException e) line. But if we use something like the below code sample, we catch the AggregateException , note that it's not a good idea since we're blocking the running thread.


3 Answers

You need to call Handle on the inner exceptions. From MSDN's documentation on Handle:

Each invocation of the predicate returns true or false to indicate whether the Exception was handled. After all invocations, if any exceptions went unhandled, all unhandled exceptions will be put into a new AggregateException which will be thrown. Otherwise, the Handle method simply returns. If any invocations of the predicate throws an exception, it will halt the processing of any more exceptions and immediately propagate the thrown exception as-is.

The example code, also from MSDN:

public static void Main()
{
    var task1 = Task.Run(() => { throw new CustomException("This exception is expected!"); });

    try 
    {
        task1.Wait();
    }
    catch (AggregateException ae)
    {
        // Call the Handle method to handle the custom exception,
        // otherwise rethrow the exception.
        ae.Handle(ex => 
        { 
            if (ex is CustomException)
                Console.WriteLine(ex.Message);
            return ex is CustomException;
        });
    }
}
like image 96
DLCross Avatar answered Oct 21 '22 18:10

DLCross


When using Parallel the "job" (here counting from 0 to 500000) gets split onto several worker threads. Each of these could throw an exception. In the sample the exception is coded to happen in the thread working on 10523. In a real world scenario more than one exception could happen (in different threads) - the AggregateException is just a "container" for all exceptions occuring while Parallel is running so that you don't lose any exception...

like image 44
Yahia Avatar answered Oct 21 '22 16:10

Yahia


AggregateException is often used for catching exceptions, that might occur when waiting for a Task to complete. Because Task in general can consist of multiple others, we do not know, whether there will be one or more exceptions thrown.

Check the following example:

// set up your task
Action<int> job = (int i) =>
{
    if (i % 100 == 0)
        throw new TimeoutException("i = " + i);
};

// we want many tasks to run in paralell
var tasks = new Task[1000];
for (var i = 0; i < 1000; i++)
{
    // assign to other variable,
    // or it will use the same number for every task
    var j = i; 
    // run your task
    var task = Task.Run(() => job(j));
    // save it
    tasks[i] = task;
}

try
{
    // wait for all the tasks to finish in a blocking manner
    Task.WaitAll(tasks);

}
catch (AggregateException e)
{
    // catch whatever was thrown
    foreach (Exception ex in e.InnerExceptions)
        Console.WriteLine(ex.Message);
}
like image 17
Kędrzu Avatar answered Oct 21 '22 16:10

Kędrzu