如何在Task中捕获异常的最佳方法?

93

使用 System.Threading.Tasks.Task<TResult>,我必须处理可能抛出的异常。我正在寻找最好的方法来做到这一点。到目前为止,我已经创建了一个基类来管理调用 .ContinueWith(...) 内部的所有未捕获的异常。

我想知道是否有更好的方式来做到这一点。或者即使这是一个好的方法去做。

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
             */

        });        
    }
}
2个回答

123
有两种方法可以实现这一点,具体取决于您使用的语言版本。
C# 5.0及以上版本
您可以使用 asyncawait 关键字来简化大量工作。 asyncawait被引入到语言中,以简化使用任务并行库,避免您使用ContinueWith,并允许您继续按照自上而下的方式编程。
因此,您可以简单地使用try/catch块来捕获异常,如下所示:
try
{
    // Start the task.
    var task = Task.Factory.StartNew<StateObject>(() => { /* action */ });

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

请注意,封装上述方法的方法必须使用async关键字,以便您可以使用await

C# 4.0及以下版本

您可以使用从TaskContinuationOptions枚举中获取值的ContinueWith重载处理异常,如下所示:

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

// For error handling.
task.ContinueWith(t => { /* error handling */ }, context,
    TaskContinuationOptions.OnlyOnFaulted);
TaskContinuationOptions 枚举的 OnlyOnFaulted 成员表示只有当前置任务抛出异常时,才会执行该后续任务。
当然,您可以对同一前置任务进行多次调用 ContinueWith,以处理非异常情况。
// 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();

1
你如何在匿名方法中知道异常类型?如果我使用 t.Exception,Intellisense 将无法显示 InnerException、Message 等属性... - guiomie
5
@guiomie 的意思是t是个例外。 - casperOne
2
上下文未定义是什么意思? - Furkan Gözükara
谢谢您的回答,我们为什么需要它?我的意思是在哪种情况下?这就足够了吗?myTask.ContinueWith(t => ErrorLogger.LogError("Error happened at func_CheckWaitingToProcessPages started task and error: " + t), TaskContinuationOptions.OnlyOnFaulted); - Furkan Gözükara
在你的例子中,“context”是什么?我使用的是.NET 4.5.2,不确定我们是否使用了相同的重载(“t”不是异常而是一个“Task”)。也许在其他版本中已经更改了。 - gdoron
显示剩余5条评论

5
你可以创建一些自定义任务工厂,这些工厂将生成带有异常处理程序的任务。就像这样:
using System;
using System.Threading.Tasks;

class FaFTaskFactory
{
    public static Task StartNew(Action action)
    {
        return Task.Factory.StartNew(action).ContinueWith(
            c =>
            {
                AggregateException exception = c.Exception;

                // Your Exception Handling Code
            },
            TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
        ).ContinueWith(
            c =>
            {
                // Your task accomplishing Code
            },
            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
        );
    }

    public static Task StartNew(Action action, Action<Task> exception_handler, Action<Task> completion_handler)
    {
        return Task.Factory.StartNew(action).ContinueWith(
            exception_handler,
            TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously
        ).ContinueWith(
            completion_handler,
            TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously
        );
    }
};

使用此工厂创建的任务,您可以在客户端代码中忽略异常处理。同时,您仍然可以等待完成此类任务或以Fire-and-Forget方式使用它们:

var task1 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); } );
var task2 = FaFTaskFactory.StartNew( () => { throw new NullReferenceException(); },
                                      c => {    Console.WriteLine("Exception!"); },
                                      c => {    Console.WriteLine("Success!"  ); } );

task1.Wait(); // You can omit this
task2.Wait(); // You can omit this

但说实话,我不太确定您为什么想要拥有完成处理代码。无论如何,这个决定取决于您的应用程序逻辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接