Table of Contents

Struct ExceptionAggregator

Namespace
Xunit.v3
Assembly
xunit.v3.core.dll

Aggregates exceptions. Intended to run one or more code blocks, and collect the exceptions thrown by those code blocks.

public struct ExceptionAggregator
Inherited Members

Constructors

ExceptionAggregator()

Initializes a new instance of the ExceptionAggregator value type.

public ExceptionAggregator()

Properties

HasExceptions

Returns true if the aggregator has at least one exception inside it.

public bool HasExceptions { get; }

Property Value

bool

Methods

Add(Exception)

Adds an exception to the aggregator.

public void Add(Exception ex)

Parameters

ex Exception

The exception to be added.

Aggregate(ExceptionAggregator)

Adds exceptions from another aggregator into this aggregator.

public void Aggregate(ExceptionAggregator aggregator)

Parameters

aggregator ExceptionAggregator

The aggregator whose exceptions should be copied.

Clear()

Clears the aggregator.

public void Clear()

Clone()

Clones the aggregator with a copy of the existing exceptions.

public ExceptionAggregator Clone()

Returns

ExceptionAggregator

Create()

Creates an empty aggregator.

public static ExceptionAggregator Create()

Returns

ExceptionAggregator

Run(Action)

Runs the code, catching the exception that is thrown and adding it to the aggregate.

public void Run(Action code)

Parameters

code Action

The code to be run.

RunAsync(Func<ValueTask>)

Runs the code, catching the exception that is thrown and adding it to the aggregate.

public ValueTask RunAsync(Func<ValueTask> code)

Parameters

code Func<ValueTask>

The code to be run.

Returns

ValueTask

RunAsync<T>(Func<ValueTask<T>>, T)

Runs the code, catching the exception that is thrown and adding it to the aggregate.

public ValueTask<T> RunAsync<T>(Func<ValueTask<T>> code, T defaultValue)

Parameters

code Func<ValueTask<T>>

The code to be run.

defaultValue T

The default value to return if the lambda throws an exception

Returns

ValueTask<T>

Type Parameters

T

Run<T>(Func<T>, T)

Runs the code, catching the exception that is thrown and adding it to the aggregate.

public T Run<T>(Func<T> code, T defaultValue)

Parameters

code Func<T>

The code to be run.

defaultValue T

The default value to return if the lambda throws an exception

Returns

T

Type Parameters

T

ThrowIfFaulted()

Throws an exception if the aggregator contains any exceptions. If the aggregator contains a single exception, it will be re-thrown without losing the original stack trace; if the aggregator contains more than one exception, then the original exceptions will be wrapped up into an instance of AggregateException.

public void ThrowIfFaulted()

ToException()

Returns an exception that represents the exceptions thrown by the code passed to the Run(Action) or RunAsync methods.

public Exception? ToException()

Returns

Exception

Returns null if no exceptions were thrown; returns the exact exception if a single exception was thrown; returns AggregateException if more than one exception was thrown.