How to: Handle Exceptions Thrown by Tasks
The following examples show how to handle exceptions thrown from one or more tasks.
In this first example, the System::AggregateException is caught and then its InnerExceptions are examined to see whether there are any exceptions that can be handled by our program code.
In this example, the System::AggregateException is caught, but no attempt is made to handle any of its inner exceptions. Instead, the Flatten method is used to extract the inner exceptions from any nested AggregateException instances and re-throw a single AggregateException that directly contains all the inner unhandled exceptions. Flattening the exception makes it more convenient for client code to handle.
To run this example, paste the code into the previous example and call RethrowAllExceptions from the Main method.