AggregateException Class

Represents one or more errors that occur during application execution.

System.Object
  System.Exception
    System.AggregateException

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
public class AggregateException : Exception

The AggregateException type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException()Initializes a new instance of the AggregateException class with a system-supplied message that describes the error.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(IEnumerable<Exception>)Initializes a new instance of the AggregateException class with references to the inner exceptions that are the cause of this exception.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(Exception[])Initializes a new instance of the AggregateException class with references to the inner exceptions that are the cause of this exception.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(String)Initializes a new instance of the AggregateException class with a specified message that describes the error.
Protected methodAggregateException(SerializationInfo, StreamingContext)Initializes a new instance of the AggregateException class with serialized data.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(String, IEnumerable<Exception>)Initializes a new instance of the AggregateException class with a specified error message and references to the inner exceptions that are the cause of this exception.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(String, Exception)Initializes a new instance of the AggregateException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAggregateException(String, Exception[])Initializes a new instance of the AggregateException class with a specified error message and references to the inner exceptions that are the cause of this exception.
Top

  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsInnerExceptionsGets a read-only collection of the Exception instances that caused the current exception.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsMessageGets a message that describes the current exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsSourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Top

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsFlattenFlattens an AggregateException instances into a single, new instance.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetBaseExceptionReturns the AggregateException that is the root cause of this exception. (Overrides Exception.GetBaseException().)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObjectDataInitializes a new instance of the AggregateException class with serialized data. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsHandleInvokes a handler on each Exception contained by this AggregateException.
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringCreates and returns a string representation of the current AggregateException. (Overrides Exception.ToString().)
Top

  NameDescription
Protected eventSerializeObjectStateOccurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.)
Top

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 additional information, see the Aggregating Exceptions entry in the .NET Matters blog. For an example, see How to: Handle Exceptions Thrown by Tasks and How to: Handle Exceptions in a PLINQ Query.

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
{
   static void 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.");
                  return true;
              }
              return false; // Let anything else stop the application.
          });
      }

      Console.WriteLine("task1 Status: {0}{1}", task1.IsCompleted ? "Completed," : "", 
                                                task1.Status);
   }

   static string[] 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)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

All public and protected members of AggregateException are thread-safe and may be used concurrently from multiple threads.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft