[英]Moq Throw async exception in one of tasks in call to Task.WhenAll
[英]Exception Handling on async tasks whenall
我正在对多个任务使用异步/等待作为参考。
public async Task DoWork() {
int[] ids = new[] { 1, 2, 3, 4, 5 };
await Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));
}
是否有可能在特定索引上捕获异常。 例如,当DoSomething在i上引发异常时,我可以捕获该特定索引吗?
一种简单的方法是捕获异常,在异常的数据上插入索引信息,然后重新抛出:
int[] ids = new[] {1, 2, 3, 4, 5};
await Task.WhenAll(ids.Select(i =>
{
try
{
return DoSomething(1, i, null);
}
catch (Exception ex)
{
ex.Data["FailedIndex"] = i; // Information about failed index
throw;
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.