Struct ExceptionAggregator
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
Methods
Add(Exception)
Adds an exception to the aggregator.
public void Add(Exception ex)
Parameters
exExceptionThe exception to be added.
Aggregate(ExceptionAggregator)
Adds exceptions from another aggregator into this aggregator.
public void Aggregate(ExceptionAggregator aggregator)
Parameters
aggregatorExceptionAggregatorThe 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
Create()
Creates an empty aggregator.
public static ExceptionAggregator Create()
Returns
Run(Action)
Runs the code, catching the exception that is thrown and adding it to the aggregate.
public void Run(Action code)
Parameters
codeActionThe 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
Returns
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
codeFunc<ValueTask<T>>The code to be run.
defaultValueTThe 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
codeFunc<T>The code to be run.
defaultValueTThe 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
nullif no exceptions were thrown; returns the exact exception if a single exception was thrown; returns AggregateException if more than one exception was thrown.