【问题标题】:Handling async exceptions处理异步异常
【发布时间】:2019-08-29 10:11:18
【问题描述】:

我想知道如何让这段代码落在catch 的PassThrough 中?

using System;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main(string[] args)
    {
        try
        {
            await PassThrough(Test());
        } catch (Exception) {
            Console.WriteLine("caught at invocation");
        }

        Console.ReadLine();
    }

    public static async Task PassThrough(Task<bool> test)
    {
        try
        {
            var result = await test.ConfigureAwait(false);

            // still need to do something with result here...
        }
        catch
        {
            Console.WriteLine("never caught... :(");
        }
    }

    /// external code!
    public static Task<bool> Test()
    {
        throw new Exception("something bad");

        // do other async stuff here
        // ...

        return Task.FromResult(true);
    }
}

fiddle

外部代码应该返回处理错误路径并返回Task.FromException?
传递Func&lt;Task&lt;bool&gt;&gt;?

【问题讨论】:

  • 如果您使用的框架版本支持它,您也应该将入口点更改为public static async Task Main()。
  • @BradleyUffner 已更新

标签: c# .net exception async-await


【解决方案1】:

我的建议是将您的PassThrough 方法更改为采用Func&lt;Task&lt;bool&gt;&gt; 而不是Task&lt;bool&gt;。这样,您可以捕获由 Test 方法的同步部分以及它启动的异步任务引起的异常。另一个优点是异步方法(使用async 和await 定义)可以直接转换为Func&lt;Task&gt; 或Func&lt;Task&lt;TResult&gt;&gt;。

using System;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main()
    {
        try
        {
            await PassThrough(Test);   
            // Note that we are now passing in a function delegate for Test,
            // equivalent to () => Test(), not its result.
        } 
        catch (Exception) 
        {
            Console.WriteLine("caught at invocation");
        }

        Console.ReadLine();
    }

    public static async Task PassThrough(Func<Task<bool>> test)
    {
        try
        {
            var task = test();   // exception thrown here
            var result = await task.ConfigureAwait(false);

            // still need to do something with result here...
        }
        catch
        {
            Console.WriteLine("caught in PassThrough");
        }
    }

    /// external code!
    public static Task<bool> Test()
    {
        throw new Exception("something bad");

        // do other async stuff here
        // ...

        return Task.FromResult(true);
    }
}

【讨论】:

    【解决方案2】:

    添加到Douglas 的答案。

    只有在您能够对异常做一些有意义的事情并且可以在该级别管理它们时才捕获异常。

    Task.FromException 基本上只是将异常放在您通常会返回的任务上。但是,在这种情况下,Async Await Pattern 已经为您完成了这项工作。即,如果您让它失败,则无论如何都会将异常置于任务上,因此您的代码似乎没有真正的理由来捕获任何东西。

    您必须考虑捕获异常的唯一相关位置是 async void,因为它们在未观察到的情况下运行,并且在引发异常时可能会导致问题

    【讨论】:

      【解决方案3】:

      在下一行中,您正在等待PassThrough,而不是Test。

      await PassThrough(Test());
      

      如果你愿意,你可以同时等待:

      await PassThrough(await Test()); // also need to change the signature of PassThrough from Task<bool> to bool.
      

      ...但在这两种情况下,Test 将首先被调用。并且由于它抛出异常,PassThrough 将永远不会被调用。这就是您看不到 “caught in PassThrough” 消息的原因。执行永远不会进入这个方法。

      【讨论】:

        猜你喜欢
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-07
        • 2013-09-10
        • 1970-01-01
        • 2011-09-11
        相关资源
        最近更新 更多