有什么方法可以让一个yield created迭代器在异常情况下继续下一个条目吗?

时间:2021-03-29 23:33:10

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?

当一个迭代器块中发生异常时,是否有办法让一个yield created的迭代器继续到下一个项目?

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;
            }
        }

2 个解决方案

#1


3  

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.

没有没有。为c#迭代器生成的代码不支持抛出异常。如果一个异常被抛出,MoveNext操作将不会完成,下一个调用将从生成的迭代器代码的立场从相同的位置重放。

#2


2  

Linq to events, aka RX, aka IObservable has explicit support for errors: http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx

Linq to events,又名RX,又名IObservable,对错误有明确的支持: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

请访问http://themechanicalbride.blogspot.com/2009/07/introduction -rx- linqto -events.html

#1


3  

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.

没有没有。为c#迭代器生成的代码不支持抛出异常。如果一个异常被抛出,MoveNext操作将不会完成,下一个调用将从生成的迭代器代码的立场从相同的位置重放。

#2


2  

Linq to events, aka RX, aka IObservable has explicit support for errors: http://msdn.microsoft.com/en-us/library/dd783449(VS.100).aspx

Linq to events,又名RX,又名IObservable,对错误有明确的支持: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

请访问http://themechanicalbride.blogspot.com/2009/07/introduction -rx- linqto -events.html