【发布时间】: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);
}
}
外部代码应该返回处理错误路径并返回Task.FromException?
传递Func<Task<bool>>?
【问题讨论】:
-
如果您使用的框架版本支持它,您也应该将入口点更改为
public static async Task Main()。 -
@BradleyUffner 已更新
标签: c# .net exception async-await