Export (0) Print
Expand All

AggregateException Class

Represents one or more errors that occur during application execution.

System.Object
  System.Exception
    System.AggregateException

Namespace:  System
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks (in System.Threading.Tasks.dll)

'Declaration
<SerializableAttribute> _
<DebuggerDisplayAttribute("Count = {InnerExceptionCount}")> _
Public Class AggregateException _
	Inherits Exception

The AggregateException type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryAggregateExceptionInitializes a new instance of the AggregateException class with a system-supplied message that describes the error.
Public methodSupported by Portable Class LibraryAggregateException(IEnumerable(Of 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 LibraryAggregateException(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 LibraryAggregateException(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 LibraryAggregateException(String, IEnumerable(Of 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 LibraryAggregateException(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 LibraryAggregateException(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 LibraryDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryInnerExceptionsGets a read-only collection of the Exception instances that caused the current exception.
Public propertySupported by Portable Class LibraryMessageGets a message that describes the current exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibrarySourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryStackTraceGets 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 LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows 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 LibraryFlattenFlattens an AggregateException instances into a single, new instance.
Public methodSupported by Portable Class LibraryGetBaseExceptionReturns the AggregateException that is the root cause of this exception. (Overrides Exception.GetBaseException.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObjectData(SerializationInfo, StreamingContext)Initializes a new instance of the AggregateException class with serialized data. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetObjectData(SerializationInfo, StreamingContext)When overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Public methodSupported by Portable Class LibraryHandleInvokes a handler on each Exception contained by this AggregateException.
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryToStringCreates 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.

Imports System.IO
Imports System.Threading.Tasks

Module Example
    Sub Main()
        ' Get a folder path whose directories should throw an UnauthorizedAccessException. 
        Dim path As String = Directory.GetParent(
                                       Environment.GetFolderPath(
                                       Environment.SpecialFolder.UserProfile)).FullName

        ' Use this line to throw UnauthorizedAccessException, which we handle. 
        Dim task1 = Task(Of String()).Factory.StartNew(Function() GetAllFiles(path))

        ' Use this line to throw an exception that is not handled. 
        ' Task task1 = Task.Factory.StartNew(Sub() Throw New IndexOutOfRangeException() ) 
        Try
            task1.Wait()
        Catch ae As AggregateException
            ae.Handle(Function(x)
                          If TypeOf (x) Is UnauthorizedAccessException Then ' 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 
                          Else 
                              Return False ' Let anything else stop the application.
                          End If 
                      End Function)
        End Try

      Console.WriteLine("task1 Status: {0}{1}", If(task1.IsCompleted, "Completed,", ""), 
                                                task1.Status)
    End Sub 

    Function GetAllFiles(ByVal str As String) As String()
        ' Should throw an UnauthorizedAccessException exception.  
        Return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories)
    End Function 
End Module

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

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

Show:
© 2015 Microsoft