NanoByte.Common
Static Public Member Functions | List of all members
NanoByte.Common.ExceptionUtils Class Reference

Provides helper methods related to Exceptions. More...

Static Public Member Functions

static string GetMessageWithInner ([NotNull] this Exception exception)
 Recursively follows the Exception.InnerExceptions and combines all their Exception.Messages, removing duplicates. More...
 
static Exception PreserveStack ([NotNull] this Exception exception)
 Configures a caught exception to preserve its original stack trace when it is rethrown. More...
 
static void ApplyWithRollback< T > ([NotNull, InstantHandle] this IEnumerable< T > elements, [NotNull, InstantHandle] Action< T > apply, [NotNull, 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 > ([NotNull, InstantHandle] this IEnumerable< T > elements, [NotNull, 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 > ([NotNull, InstantHandle] 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...
 

Detailed Description

Provides helper methods related to Exceptions.

Member Function Documentation

◆ ApplyWithRollback< T >()

static void NanoByte.Common.ExceptionUtils.ApplyWithRollback< T > ( [NotNull, InstantHandle] this IEnumerable< T >  elements,
[NotNull, InstantHandle] Action< T >  apply,
[NotNull, 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.

◆ GetMessageWithInner()

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

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

◆ PreserveStack()

static Exception NanoByte.Common.ExceptionUtils.PreserveStack ( [NotNull] this Exception  exception)
inlinestatic

Configures a caught exception to preserve its original stack trace when it is rethrown.

This has no effect on platforms that do not support exception serialization.

◆ Retry< TException >()

static void NanoByte.Common.ExceptionUtils.Retry< TException > ( [NotNull, InstantHandle] 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 

◆ TryAny< T >()

static void NanoByte.Common.ExceptionUtils.TryAny< T > ( [NotNull, InstantHandle] this IEnumerable< T >  elements,
[NotNull, 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 .

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