有什么方法可以让创建的迭代器在出现异常时继续执行下一项吗?
当其中一个迭代器块内发生异常时,是否有任何方法可以让创建的迭代器继续执行下一项?
目前这不起作用:
Boolean result;
while (true)
{
try
{
result = enumerator.MoveNext(); //Taken from a yield created enumerable
if (!result) break;
}
catch (Exception ex)
{
Console.WriteLine("CATCHED...");
continue;
}
}
Is there any way to have a yield created iterator continue to the next item when an exception occurs inside one of the iterator blocks?
This is currently not working:
Boolean result;
while (true)
{
try
{
result = enumerator.MoveNext(); //Taken from a yield created enumerable
if (!result) break;
}
catch (Exception ex)
{
Console.WriteLine("CATCHED...");
continue;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有。 C# 迭代器生成的代码不支持引发异常。如果引发异常,则 MoveNext 操作将无法完成,并且从生成的迭代器代码的角度来看,下一个调用将从同一位置重播。
No there is not. The generated code for a C# iterator does not support exceptions being thrown. If an exception is thrown the MoveNext operation will not complete and the next call will replay from the same place from the standpoint of the generated iterator code.
Linq to events,又名 RX,又名 IObservable 明确支持错误: http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx
请访问 http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html
Linq to events, aka RX, aka IObservable has explicit support for errors: http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx
Check it out at http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-events.html