/// <summary>
        /// Throws an exception to handle a task that completed in a state other than
        /// <see cref="TaskStatus.RanToCompletion"/>.
        /// </summary>
        /// <param name="task">The task.</param>
        private static void ThrowForNonSuccess(Task task)
        {
            Debug.Assert(task.Status != TaskStatus.RanToCompletion, "Assertion failed: task.Status != TaskStatus.RanToCompletion");

            // Handle whether the task has been canceled or faulted
            switch (task.Status)
            {
            // If the task completed in a canceled state, throw an OperationCanceledException. TaskCanceledException
            // derives from OCE, and by throwing it we automatically pick up the completed task's CancellationToken if
            // it has one, including that CT in the OCE.
            case TaskStatus.Canceled:
                throw new TaskCanceledException(task);

            // If the task faulted, throw its first exception, even if it contained more than one.
            case TaskStatus.Faulted:
                throw AsyncServices.PrepareExceptionForRethrow(task.Exception.InnerException);

            // This should not happen on valid usage.
            default:
                throw new InvalidOperationException(InvalidOperationExceptionTaskNotCompleted);
            }
        }