The following example catches the AggregateException exception and calls the Handle method to handle each exception it contains. Compiling and running the example with the first task1 variable should result in an AggregateException object that contains an UnauthorizedAccessException exception. Commenting out that line, uncommenting the second task1 variable, and compiling and running the example produces an AggregateException object that contains an IndexOutOfRangeException exception.

using System;
using System.IO;
using System.Threading.Tasks;
class Example
{
staticvoid Main(string[] args)
{
// Get a folder path whose directories should throw an UnauthorizedAccessException.string path = Directory.GetParent(
Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile)).FullName;
// Use this line to throw UnauthorizedAccessException, which we handle.
Task<string[]> task1 = Task<string[]>.Factory.StartNew(() => GetAllFiles(path));
// Use this line to throw an exception that is not handled.// Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); } );try {
task1.Wait();
}
catch (AggregateException ae) {
ae.Handle((x) =>
{
if (x is UnauthorizedAccessException) // This we know how to handle.
{
Console.WriteLine("You do not have permission to access all folders in this path.");
Console.WriteLine("See your network administrator or try another path.");
returntrue;
}
returnfalse; // Let anything else stop the application.
});
}
Console.WriteLine("task1 Status: {0}{1}", task1.IsCompleted ? "Completed," : "",
task1.Status);
}
staticstring[] GetAllFiles(string str)
{
// Should throw an UnauthorizedAccessException exception.return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories);
}
}
// The example displays the following output if the file access task is run:// You do not have permission to access all folders in this path.// See your network administrator or try another path.// task1 Status: Completed,Faulted// It displays the following output if the second task is run:// Unhandled Exception: System.AggregateException: One or more errors occurred. ---// > System.IndexOutOfRangeException: Index was outside the bounds of the array.// at Example.<Main>b__0()// at System.Threading.Tasks.Task.Execute()// --- End of inner exception stack trace ---// at System.AggregateException.Handle(Func`2 predicate)// at Example.Main(String[] args)