[英]Confusion over AggregateException Handle method
ReSharper was giving me a CoVariantConversion warning so I decided to google this and see how to fix it. ReSharper给了我一个CoVariantConversion警告,所以我决定谷歌这个,看看如何解决它。 I came accross this snippet of code:
我遇到了这段代码:
// ReSharper disable CoVariantArrayConversion
try
{
Task.WaitAll(taskList.ToArray());
}
catch (AggregateException ex)
{
ex.Handle(e => true);
}
// ReSharper restore CoVariantArrayConversion
This part is confusing me: 这部分令我困惑:
ex.Handle(e => true);
What does it do? 它有什么作用? I would think that it does nothing.
我认为它什么都不做。
You are correct: the line can be removed and have the same effect (causing all the exceptions to be considered "handled") as if the line was there. 你是对的:可以删除该行并具有相同的效果(导致所有异常被视为“已处理”),就好像该行在那里一样。
The only time it would be useful is if the lambda could return false for some exceptions (which it doesn't in this case). 它唯一有用的时候是lambda可能会因某些异常而返回false(在这种情况下它不会)。
这就是说,处理异常,没有别的。
Here is a sample that Shows how the Handle method could be used: 这是一个示例,说明如何使用Handle方法:
Task task = Task.Factory.StartNew(() =>
{
throw new UnauthorizedAccessException();
});
try
{
task.Wait();
}
catch (AggregateException ex)
{
ex.Handle(x =>
{
if (x is UnauthorizedAccessException)
{
// Handle this exception...
return true;
}
// Other exceptions will not be handled here.
return false;
});
}
The sample comes from this article: Asynchronous Programming - Exception Handling 该示例来自本文: 异步编程 - 异常处理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.