NanoByte.Common  2.9.0
NanoByte.Common.ExceptionUtils Class Reference

Provides helper methods related to Exceptions. More...

Static Public Member Functions

static string GetMessageWithInner (this Exception exception)
 Recursively follows the Exception.InnerExceptions and combines all their Exception.Messages, removing duplicates. More...
 
static Exception Rethrow (this Exception exception)
 Rethrows an exception while preserving its original stack trace. More...
 
static void ApplyWithRollback< T > ([InstantHandle] this IEnumerable< T > elements, [InstantHandle] Action< T > apply, [InstantHandle] Action< T > rollback)
 Applies an operation for all elements of a collection. Automatically applies rollback operations in case of an exception. More...
 
static void TryAny< T > ([InstantHandle] this IEnumerable< T > elements, [InstantHandle] Action< T > action)
 Applies an operation for the first possible element of a collection. If the operation succeeds the remaining elements are ignored. If the operation fails it is repeated for the next element. More...
 
static void Retry< TException > (RetryAction action, int maxRetries=2)
 Executes a delegate and automatically retries it using exponential back-off if a specific type of exception was raised. More...
 
static async Task ApplyWithRollbackAsync< T > (this IEnumerable< T > elements, Func< T, Task > apply, Func< T, Task > rollback)
 Applies an operation for all elements of a collection. Automatically applies rollback operations in case of an exception. More...
 
static async Task TryAnyAsync< T > (this IEnumerable< T > elements, Func< T, Task > action)
 Applies an operation for the first possible element of a collection. If the operation succeeds the remaining elements are ignored. If the operation fails it is repeated for the next element. More...
 
static async Task RetryAsync< TException > (RetryAsyncAction action, int maxRetries=2)
 Executes an asynchronous delegate and automatically retries it using exponential back-off if a specific type of exception was raised. More...
 

Detailed Description

Provides helper methods related to Exceptions.

Member Function Documentation

◆ ApplyWithRollback< T >()

static void NanoByte.Common.ExceptionUtils.ApplyWithRollback< T > ( [InstantHandle] this IEnumerable< T >  elements,
[InstantHandle] Action< T >  apply,
[InstantHandle] Action< T >  rollback 
)
inlinestatic

Applies an operation for all elements of a collection. Automatically applies rollback operations in case of an exception.

Template Parameters
TThe type of elements to operate on.
Parameters
elementsThe elements to apply the action for.
applyThe action to apply to each element.
rollbackThe action to apply to each element that apply was called on in case of an exception.

rollback is applied to the element that raised an exception in apply and then iterating backwards through all previous elements. After rollback is complete the exception is passed on.

◆ ApplyWithRollbackAsync< T >()

static async Task NanoByte.Common.ExceptionUtils.ApplyWithRollbackAsync< T > ( this IEnumerable< T >  elements,
Func< T, Task >  apply,
Func< T, Task >  rollback 
)
inlinestatic

Applies an operation for all elements of a collection. Automatically applies rollback operations in case of an exception.

Template Parameters
TThe type of elements to operate on.
Parameters
elementsThe elements to apply the action for.
applyThe action to apply to each element.
rollbackThe action to apply to each element that apply was called on in case of an exception.

rollback is applied to the element that raised an exception in apply and then iterating backwards through all previous elements. After rollback is complete the exception is passed on.

◆ GetMessageWithInner()

static string NanoByte.Common.ExceptionUtils.GetMessageWithInner ( this Exception  exception)
inlinestatic

Recursively follows the Exception.InnerExceptions and combines all their Exception.Messages, removing duplicates.

◆ Rethrow()

static Exception NanoByte.Common.ExceptionUtils.Rethrow ( this Exception  exception)
inlinestatic

Rethrows an exception while preserving its original stack trace.

Returns
This method never returns. You can "throw" the return value to satisfy the compiler's flow analysis if necessary.

◆ Retry< TException >()

static void NanoByte.Common.ExceptionUtils.Retry< TException > ( RetryAction  action,
int  maxRetries = 2 
)
inlinestatic

Executes a delegate and automatically retries it using exponential back-off if a specific type of exception was raised.

Template Parameters
TExceptionThe type of exception to trigger a retry.
Parameters
actionThe action to execute.
maxRetriesThe maximum number of retries to attempt.
Type Constraints
TException :Exception 

◆ RetryAsync< TException >()

static async Task NanoByte.Common.ExceptionUtils.RetryAsync< TException > ( RetryAsyncAction  action,
int  maxRetries = 2 
)
inlinestatic

Executes an asynchronous delegate and automatically retries it using exponential back-off if a specific type of exception was raised.

Template Parameters
TExceptionThe type of exception to trigger a retry.
Parameters
actionThe action to execute.
maxRetriesThe maximum number of retries to attempt.
Type Constraints
TException :Exception 

◆ TryAny< T >()

static void NanoByte.Common.ExceptionUtils.TryAny< T > ( [InstantHandle] this IEnumerable< T >  elements,
[InstantHandle] Action< T >  action 
)
inlinestatic

Applies an operation for the first possible element of a collection. If the operation succeeds the remaining elements are ignored. If the operation fails it is repeated for the next element.

Template Parameters
TThe type of elements to operate on.
Parameters
elementsThe elements to apply the action for.
actionThe action to apply to an element.
Exceptions
ExceptionThe exception thrown by action for the last element of elements .

◆ TryAnyAsync< T >()

static async Task NanoByte.Common.ExceptionUtils.TryAnyAsync< T > ( this IEnumerable< T >  elements,
Func< T, Task >  action 
)
inlinestatic

Applies an operation for the first possible element of a collection. If the operation succeeds the remaining elements are ignored. If the operation fails it is repeated for the next element.

Template Parameters
TThe type of elements to operate on.
Parameters
elementsThe elements to apply the action for.
actionThe action to apply to an element.
Exceptions
ExceptionThe exception thrown by action for the last element of elements .

The documentation for this class was generated from the following file: