What is the best way to catch exception in Task?

JiBéDoublevé picture JiBéDoublevé · Oct 19, 2012 · Viewed 80.2k times · Source

With System.Threading.Tasks.Task<TResult>, I have to manage the exceptions that could be thrown. I'm looking for the best way to do that. So far, I've created a base class that manages all the uncaught exceptions inside the call of .ContinueWith(...)

I'm wondering if there's a better way do do that. Or even if it is a good way to do that.

public class BaseClass
{
    protected void ExecuteIfTaskIsNotFaulted<T>(Task<T> e, Action action)
    {
        if (!e.IsFaulted) { action(); }
        else
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                /* I display a window explaining the error in the GUI 
                 * and I log the error.
                 */
                this.Handle.Error(e.Exception);
            }));            
        }
    }
}   

public class ChildClass : BaseClass
{
    public void DoItInAThread()
    {
        var context = TaskScheduler.FromCurrentSynchronizationContext();
        Task.Factory.StartNew<StateObject>(() => this.Action())
                    .ContinueWith(e => this.ContinuedAction(e), context);
    }

    private void ContinuedAction(Task<StateObject> e)
    {
        this.ExecuteIfTaskIsNotFaulted(e, () =>
        {
            /* The action to execute 
             * I do stuff with e.Result
             */

        });        
    }
}

Answer

casperOne picture casperOne · Oct 19, 2012

There are two ways you can do this, dependent on the version of the language you are using.

C# 5.0 and above

You can use the async and await keywords to simplify a great deal of this for you.

async and await were introduced into the language to simplify using the Task Parallel Library, preventing you from having to use ContinueWith and allowing you to continue to program in a top-down manner.

Because of this, you can simply use a try/catch block to catch the exception, like so:

try
{
    // Start the task.
    var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });

    // Await the task.
    await task;
}
catch (Exception e)
{
    // Perform cleanup here.
}

Note that the method encapsulating the above must use have the async keyword applied so you can use await.

C# 4.0 and below

You can handle exceptions using the ContinueWith overload that takes a value from the TaskContinuationOptions enumeration, like so:

// Get the task.
var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });

// For error handling.
task.ContinueWith(t => { /* error handling */ }, context,
    TaskContinuationOptions.OnlyOnFaulted);

The OnlyOnFaulted member of the TaskContinuationOptions enumeration indicates that the continuation should only be executed if the antecedent task threw an exception.

Of course, you can have more than one call to ContinueWith off the same antecedent, handling the non-exceptional case:

// Get the task.
var task = new Task<StateObject>(() => { /* action */ });

// For error handling.
task.ContinueWith(t => { /* error handling */ }, context, 
    TaskContinuationOptions.OnlyOnFaulted);

// If it succeeded.
task.ContinueWith(t => { /* on success */ }, context,
    TaskContinuationOptions.OnlyOnRanToCompletion);

// Run task.
task.Start();