System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Exception
[System.Runtime.CompilerServices.Nullable(0)]
[<System.Runtime.CompilerServices.Nullable(0)>]
[System.Diagnostics.DebuggerDisplay("Count = {InnerExceptions.Count}")]
[<System.Diagnostics.DebuggerDisplay("Count = {InnerExceptions.Count}")>]
[System.Serializable]
[<System.Serializable>]
[System.Diagnostics.DebuggerDisplay("Count = {InnerExceptionCount}")]
[<System.Diagnostics.DebuggerDisplay("Count = {InnerExceptionCount}")>]
Represents one or more errors that occur during application execution.
is used to consolidate multiple failures into a single, throwable exception object. It is used extensively in the [Task Parallel Library (TPL)](/dotnet/standard/parallel-programming/task-parallel-library-tpl) and [Parallel LINQ (PLINQ)](/dotnet/standard/parallel-programming/introduction-to-plinq). For more information, see [Exception Handling](/dotnet/standard/parallel-programming/exception-handling-task-parallel-library) and [How to: Handle Exceptions in a PLINQ Query](/dotnet/standard/parallel-programming/how-to-handle-exceptions-in-a-plinq-query). For additional information, see the [Aggregating Exceptions](https://learn.microsoft.com/archive/msdn-magazine/2009/brownfield/aggregating-exceptions) entry in the .NET Matters blog.
## Examples
The following example catches the exception and calls the method to handle each exception it contains. Compiling and running the example with the first `task1` variable should result in an object that contains an exception. Commenting out that line, uncommenting the second `task1` variable, and compiling and running the example produces an object that contains an exception.
:::code language="csharp" source="~/snippets/csharp/System/AggregateException/Overview/exception1.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_CLR_System/system.aggregateexception.class/fs/exception1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/System/AggregateException/Overview/exception1.vb" id="Snippet1":::
]]>
All public and protected members of are thread-safe and may be used concurrently from multiple threads.
System.Threading.Tasks
4.0.0.0
4.0.10.0
Initializes a new instance of the class.
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
Initializes a new instance of the class with a system-supplied message that describes the error.
property of the new instance to a system-supplied message that describes the error, such as "System.AggregateException: One or more errors occurred." This message takes into account the current system culture.
The following table shows the initial property values for an instance of .
|Property|Value|
|--------------|-----------|
||`null`.|
||The localized error message string.|
]]>
Handling and Throwing Exceptions
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
The exceptions that are the cause of the current exception.
Initializes a new instance of the class with references to the inner exceptions that are the cause of this exception.
To be added.
The argument is null.
An element of is null.
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.ParamArray]
[<System.ParamArray>]
The exceptions that are the cause of the current exception.
Initializes a new instance of the class with references to the inner exceptions that are the cause of this exception.
To be added.
The argument is null.
An element of is null.
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
The message that describes the exception. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
Initializes a new instance of the class with a specified message that describes the error.
.
|Property|Value|
|--------------|-----------|
||`null`.|
||The error message string specified in `message`.|
]]>
Handling and Throwing Exceptions
Constructor
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Threading.Tasks
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>]
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
The object that holds the serialized object data.
The contextual information about the source or destination.
Initializes a new instance of the class with serialized data.
The argument is null.
The exception could not be deserialized correctly.
XML and SOAP Serialization
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The error message that explains the reason for the exception.
The exceptions that are the cause of the current exception.
Initializes a new instance of the class with a specified error message and references to the inner exceptions that are the cause of this exception.
To be added.
The argument is null.
An element of is null.
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
The message that describes the exception. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
The exception that is the cause of the current exception. If the parameter is not , the current exception is raised in a block that handles the inner exception.
Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.
property. The property returns the same value that is passed into the constructor, or `null` if the property does not supply the inner exception value to the constructor.
The following table shows the initial property values for an instance of .
|Property|Value|
|--------------|-----------|
||`null`.|
||The error message string specified in `message`.|
]]>
The argument is null.
Handling and Throwing Exceptions
Constructor
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
[System.Runtime.CompilerServices.Nullable(2)]
[<System.Runtime.CompilerServices.Nullable(2)>]
[System.ParamArray]
[<System.ParamArray>]
The error message that explains the reason for the exception.
The exceptions that are the cause of the current exception.
Initializes a new instance of the class with a specified error message and references to the inner exceptions that are the cause of this exception.
To be added.
The argument is null.
An element of is null.
Method
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.AggregateException
Flattens an instances into a single, new instance.
A new, flattened .
exception before it is propagated to the parent task, which wraps that exception in its own exception before it propagates it back to the calling thread. In such cases, the property of the exception that is caught by the , , or method contains one or more instances, not the original exceptions that caused the fault. To avoid having to iterate over nested exceptions, you can use the method to remove all the nested exceptions, so that the property of the returned object contains the original exceptions.
This method recursively flattens all instances of exceptions that are inner exceptions of the current instance. The inner exceptions returned in the new are the union of all the inner exceptions from exception tree rooted at the current instance.
## Examples
In the following example, nested instances are flattened and handled in just one loop.
:::code language="csharp" source="~/snippets/csharp/System/AggregateException/Flatten/flatten2.cs" id="Snippet22":::
:::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_Misc/tpl_exceptions/fs/flatten2.fs" id="Snippet22":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Misc/tpl_exceptions/vb/flatten2.vb" id="Snippet22":::
You can also use the method to rethrow the inner exceptions from multiple instances thrown by multiple tasks in a single instance, as the following example shows.
:::code language="csharp" source="~/snippets/csharp/System/AggregateException/Flatten/taskexceptions2.cs" id="Snippet13":::
:::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_Misc/tpl_exceptions/fs/taskexceptions2.fs" id="Snippet13":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Misc/tpl_exceptions/vb/taskexceptions2.vb" id="Snippet13":::
]]>
Exception Handling (Task Parallel Library)
Aggregating Exceptions
Method
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Exception
Returns the that is the root cause of this exception. This exception is either the root exception or the first that contains either multiple inner exceptions or no inner exceptions at all.
The that is the root cause of this exception.
This will either be the root exception, or the first that contains either multiple inner exceptions or no inner exceptions at all.
Method
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Threading.Tasks
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>]
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
[System.Security.SecurityCritical]
[<System.Security.SecurityCritical>]
System.Void
The object that holds the serialized object data.
The contextual information about the source or destination.
Initializes a new instance of the class with serialized data.
The argument is null.
XML and SOAP Serialization
Method
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Void
The predicate to execute for each exception. The predicate accepts as an argument the to be processed and returns a Boolean to indicate whether the exception was handled.
Invokes a handler on each contained by this .
was handled. After all invocations, any unhandled exceptions are put into a new that will be thrown. If there are no unhandled exceptions, the 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.
## Examples
Ordinarily, an exception handler that catches an exception uses a `foreach` loop (in C#) or `For Each` loop (in Visual Basic) to handle each exception in its collection. Instead, the following example uses the method to handle each exception, and only rethrows exceptions that are not `CustomException` instances.
:::code language="csharp" source="~/snippets/csharp/System/AggregateException/Handle/handlemethod2.cs" id="Snippet16":::
:::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_Misc/tpl_exceptions/fs/handlemethod2.fs" id="Snippet16":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Misc/tpl_exceptions/vb/handlemethod2.vb" id="Snippet16":::
The following is a more complete example that uses the method to provide special handling for an when enumerating files.
:::code language="csharp" source="~/snippets/csharp/System/AggregateException/Handle/taskexceptions.cs" id="Snippet12":::
:::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_Misc/tpl_exceptions/fs/taskexceptions.fs" id="Snippet12":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Misc/tpl_exceptions/vb/taskexceptions.vb" id="Snippet12":::
]]>
The argument is null.
An exception contained by this was not handled.
Property
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.Collections.ObjectModel.ReadOnlyCollection<System.Exception>
Gets a read-only collection of the instances that caused the current exception.
A read-only collection of the instances that caused the current exception.
To be added.
Property
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
mscorlib
netstandard
2.1.0.0
System.Threading.Tasks
System.String
Gets a message that describes the exception.
The message that describes the exception.
To be added.
Method
System.Threading.Tasks
4.0.0.0
4.0.10.0
mscorlib
2.0.5.0
4.0.0.0
netstandard
2.0.0.0
2.1.0.0
System.Runtime
4.2.0.0
4.2.1.0
4.2.2.0
5.0.0.0
6.0.0.0
7.0.0.0
8.0.0.0
9.0.0.0
10.0.0.0
11.0.0.0
System.String
Creates and returns a string representation of the current .
A string representation of the current exception.
To be added.