src/NAsync/TaskExtensions.cs
/// <summary>
/// Catches any Exception of type TException bubbling up from the Task and allow the user to filter out the exception.
/// Remark: You cannot filter the OperationCanceledException thrown on task cancellation, if the the first Task has been built with the token (i.e. by calling Then(second, token)).
/// 
/// Be aware that successive .Catch&lt;Ex>() corresponds 
/// to the following synchronous syntax:
/// <example>
///  try
///  {
///      try
///      {
///          // SYNC TASK
///      }
///      catch (Ex1 e)
///      {
///          // FIRST CATCH BLOCK
///      }
///  }
///  catch (Ex2 e)
///  {
///      // SECOND CATCH, MAY CATCH THROWN EXCEPTION BY FIRST CATCH BLOCK
///  }
/// </example>
/// </summary>
Catches any Exception of type TException bubbling up from the Task and allow the user to filter out the exception.
Remark: You cannot filter the OperationCanceledException thrown on task cancellation, if the the first Task has been built with the token ).

Be aware that successive .Catch() corresponds
to the following synchronous syntax.


Catches any Exception of type TException bubbling up from the Task and allow the user to filter out the exception . Remark : You cannot filter the OperationCanceledException thrown on task cancellation if the the first Task has been built with the token ) . Be aware that successive . Catch () corresponds to the following synchronous syntax .
[NotNull]
        public static Task Catch<TException>([NotNull] this Task first,
            [NotNull] Action<TException> next,
            [CanBeNull] TaskScheduler taskScheduler = null)
            where TException : Exception
        {
            if (first == null) throw new ArgumentNullException("first");
            if (next == null) throw new ArgumentNullException("next");
            TakeOverResponsabilityForUnobservedException(first);
            var tcs = new TaskCompletionSource<object>();
            first.ContinueWith(delegate
            {
                if (first.IsFaulted)
                {
                    var concernedException = first.Exception.InnerException as TException;
                    if (concernedException == null)
                    {
                        tcs.TrySetException(first.Exception.InnerExceptions); 
                    }
                    else
                    {
                        try
                        {
                            next(concernedException);
                            tcs.SetResult(null);
                        }
                        catch (Exception e)
                        {
                            tcs.TrySetException(e);
                        }
                    }
                }
                else if (first.IsCanceled) tcs.TrySetCanceled(); 
                else tcs.SetResult(null); 
            },
                taskScheduler ?? GetDefaultTaskScheduler());
            return tcs.Task;
        }
[ NotNull ] public static Task Catch < TException > ( [ NotNull ] this Task first , [ NotNull ] Action < TException > next , [ CanBeNull ] TaskScheduler taskScheduler = null ) where TException : Exception { if ( first == null ) throw new ArgumentNullException ( " " ) ; if ( next == null ) throw new ArgumentNullException ( " " ) ; TakeOverResponsabilityForUnobservedException ( first ) ; var tcs = new TaskCompletionSource < object > ( ) ; first . ContinueWith ( delegate { if ( first . IsFaulted ) { var concernedException = first . Exception . InnerException as TException ; if ( concernedException == null ) { tcs . TrySetException ( first . Exception . InnerExceptions ) ; } else { try { next ( concernedException ) ; tcs . SetResult ( null ) ; } catch ( Exception e ) { tcs . TrySetException ( e ) ; } } } else if ( first . IsCanceled ) tcs . TrySetCanceled ( ) ; else tcs . SetResult ( null ) ; } , taskScheduler ?? GetDefaultTaskScheduler ( ) ) ; return tcs . Task ; }
Catches any Exception of type TException bubbling up from the Task and allow the user to filter out the exception.
Catches any Exception of type TException bubbling up from the Task and allow the user to filter out the exception .
// ReSharper disable once PossibleNullReferenceException // ReSharper disable once PossibleNullReferenceException // not concerned -> forward    // handle the exception // forward // forward
this
[NotNull]
Action<TException>
[NotNull]
TaskScheduler
[CanBeNull]