CoreTaskExtensions.Catch<TException, TResult> Method (Task<TResult>, Predicate<TException>, Func<Task<TResult>, TException, TResult>) |
Namespace: Rackspace.Threading
public static Task<TResult> Catch<TException, TResult>( this Task<TResult> task, Predicate<TException> filter, Func<Task<TResult>, TException, TResult> handler ) where TException : Exception
Exception | Condition |
---|---|
ArgumentNullException |
If task is null. -or- If filter is null. -or- If handler is null. |
This code implements support for the following construct without requiring the use of async/await.
try { return await task.ConfigureAwait(false); } catch (TException ex) when (filter(ex)) { return handler(task, ex); }
This method is capable of handling Canceled antecedent tasks when TException is assignable from TaskCanceledException.
This method ensures that exception information provided by a faulted or canceled task is not wrapped in an additional AggregateException.
![]() |
---|
Since the continuation is executed synchronously, this method should only be used for lightweight exception handler methods. For non-trivial exception handlers, use a Task for the exception handling operation and call Catch<TException, TResult>(Task<TResult>, Predicate<TException>, Func<Task<TResult>, TException, Task<TResult>>) instead. |