Show / Hide Table of Contents

Class ExceptionUtils

Provides helper methods related to Exceptions.

Inheritance
Object
ExceptionUtils
Namespace: NanoByte.Common
Assembly: NanoByte.Common.dll
Syntax
public static class ExceptionUtils : Object

Methods

GetMessageWithInner(Exception)

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

Declaration
public static string GetMessageWithInner(this Exception exception)
Parameters
Type Name Description
Exception exception
Returns
Type Description
String

Rethrow(Exception)

Rethrows an exception while preserving its original stack trace.

Declaration
public static Exception Rethrow(this Exception exception)
Parameters
Type Name Description
Exception exception
Returns
Type Description
Exception

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

RethrowFirstInner(AggregateException)

Rethrows the first of the InnerExceptions and logs all others.

Declaration
public static Exception RethrowFirstInner(this AggregateException exception)
Parameters
Type Name Description
AggregateException exception
Returns
Type Description
Exception

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

Retry<TException>(Action, Int32)

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

Declaration
public static void Retry<TException>(Action action, int maxRetries = 2)
    where TException : Exception
Parameters
Type Name Description
Action action

The action to execute.

Int32 maxRetries

The maximum number of retries to attempt.

Type Parameters
Name Description
TException

The type of exception to trigger a retry.

Retry<TException, TResult>(Func<TResult>, Int32)

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

Declaration
public static TResult Retry<TException, TResult>(Func<TResult> function, int maxRetries = 2)
    where TException : Exception
Parameters
Type Name Description
Func<TResult> function

The function to execute.

Int32 maxRetries

The maximum number of retries to attempt.

Returns
Type Description
TResult

The result of function.

Type Parameters
Name Description
TException

The type of exception to trigger a retry.

TResult

The type of result the function produces.

Retry<TException, TResult>(Func<Task<TResult>>, Int32)

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

Declaration
public static Task<TResult> Retry<TException, TResult>(Func<Task<TResult>> function, int maxRetries = 2)
    where TException : Exception
Parameters
Type Name Description
Func<Task<TResult>> function

The function to execute.

Int32 maxRetries

The maximum number of retries to attempt.

Returns
Type Description
Task<TResult>

The result of function.

Type Parameters
Name Description
TException

The type of exception to trigger a retry.

TResult

The type of result the function produces.

RetryAsync<TException>(Func<Task>, Int32)

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

Declaration
public static Task RetryAsync<TException>(Func<Task> action, int maxRetries = 2)
    where TException : Exception
Parameters
Type Name Description
Func<Task> action

The action to execute.

Int32 maxRetries

The maximum number of retries to attempt.

Returns
Type Description
Task
Type Parameters
Name Description
TException

The type of exception to trigger a retry.

TryAny<T>(IEnumerable<T>, Action<T>)

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. If the operation fails with OperationCanceledExceptions no further elements are tried.

Declaration
public static void TryAny<T>(this IEnumerable<T> elements, Action<T> action)
Parameters
Type Name Description
IEnumerable<T> elements

The elements to apply the action for.

Action<T> action

The action to apply to an element.

Type Parameters
Name Description
T

The type of elements to operate on.

Exceptions
Type Condition
Exception

The exception thrown by action for the last element of elements.

TryAnyAsync<T>(IEnumerable<T>, Func<T, Task>)

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.

Declaration
public static Task TryAnyAsync<T>(this IEnumerable<T> elements, Func<T, Task> action)
Parameters
Type Name Description
IEnumerable<T> elements

The elements to apply the action for.

Func<T, Task> action

The action to apply to an element.

Returns
Type Description
Task
Type Parameters
Name Description
T

The type of elements to operate on.

Exceptions
Type Condition
Exception

The exception thrown by action for the last element of elements.

In This Article
Back to top Copyright Bastian Eicher