Table of Contents

Class ExceptionUtils

Namespace
NanoByte.Common
Assembly
NanoByte.Common.dll

Provides helper methods related to System.Exceptions.

public static class ExceptionUtils
Inheritance
object
ExceptionUtils

Methods

GetMessageWithInner(Exception)

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

[Pure]
public static string GetMessageWithInner(this Exception exception)

Parameters

exception Exception

Returns

string

Rethrow(Exception)

Rethrows an exception while preserving its original stack trace.

public static Exception Rethrow(this Exception exception)

Parameters

exception Exception

Returns

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 System.AggregateException.InnerExceptions and logs all others.

public static Exception RethrowFirstInner(this AggregateException exception)

Parameters

exception AggregateException

Returns

Exception

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

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

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

public static Task RetryAsync<TException>(Func<Task> action, int maxRetries = 2) where TException : Exception

Parameters

action Func<Task>

The action to execute.

maxRetries int

The maximum number of retries to attempt.

Returns

Task

Type Parameters

TException

The type of exception to trigger a retry.

Retry<TException>(Action, int)

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

public static void Retry<TException>(Action action, int maxRetries = 2) where TException : Exception

Parameters

action Action

The action to execute.

maxRetries int

The maximum number of retries to attempt.

Type Parameters

TException

The type of exception to trigger a retry.

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

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

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

Parameters

function Func<Task<TResult>>

The function to execute.

maxRetries int

The maximum number of retries to attempt.

Returns

Task<TResult>

The result of function.

Type Parameters

TException

The type of exception to trigger a retry.

TResult

The type of result the function produces.

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

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

public static TResult Retry<TException, TResult>(Func<TResult> function, int maxRetries = 2) where TException : Exception

Parameters

function Func<TResult>

The function to execute.

maxRetries int

The maximum number of retries to attempt.

Returns

TResult

The result of function.

Type Parameters

TException

The type of exception to trigger a retry.

TResult

The type of result the function produces.

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.

public static Task TryAnyAsync<T>(this IEnumerable<T> elements, Func<T, Task> action)

Parameters

elements IEnumerable<T>

The elements to apply the action for.

action Func<T, Task>

The action to apply to an element.

Returns

Task

Type Parameters

T

The type of elements to operate on.

Exceptions

Exception

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

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 System.OperationCanceledExceptions no further elements are tried.

public static void TryAny<T>(this IEnumerable<T> elements, Action<T> action)

Parameters

elements IEnumerable<T>

The elements to apply the action for.

action Action<T>

The action to apply to an element.

Type Parameters

T

The type of elements to operate on.

Exceptions

Exception

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