Class ExceptionUtils
Provides helper methods related to System.Exceptions.
public static class ExceptionUtils
- Inheritance
-
objectExceptionUtils
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
exceptionException
Returns
- string
Rethrow(Exception)
Rethrows an exception while preserving its original stack trace.
public static Exception Rethrow(this Exception exception)
Parameters
exceptionException
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
exceptionAggregateException
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
actionFunc<Task>The action to execute.
maxRetriesintThe maximum number of retries to attempt.
Returns
- Task
Type Parameters
TExceptionThe 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
actionActionThe action to execute.
maxRetriesintThe maximum number of retries to attempt.
Type Parameters
TExceptionThe 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
functionFunc<Task<TResult>>The function to execute.
maxRetriesintThe maximum number of retries to attempt.
Returns
- Task<TResult>
The result of
function.
Type Parameters
TExceptionThe type of exception to trigger a retry.
TResultThe type of result the
functionproduces.
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
functionFunc<TResult>The function to execute.
maxRetriesintThe maximum number of retries to attempt.
Returns
- TResult
The result of
function.
Type Parameters
TExceptionThe type of exception to trigger a retry.
TResultThe type of result the
functionproduces.
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
elementsIEnumerable<T>The elements to apply the action for.
actionFunc<T, Task>The action to apply to an element.
Returns
- Task
Type Parameters
TThe type of elements to operate on.
Exceptions
- Exception
The exception thrown by
actionfor the last element ofelements.
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
elementsIEnumerable<T>The elements to apply the action for.
actionAction<T>The action to apply to an element.
Type Parameters
TThe type of elements to operate on.
Exceptions
- Exception
The exception thrown by
actionfor the last element ofelements.