C#: foreach breaking when the IEnumerator.MoveNext method throws exceptions #7819
-
DescriptionWhen you iterate through a collection using the Reproduction StepsIt is difficult to provide an example because, usually exception causing member is unexpected in the collection. One of the possible sources could be a enumerable returned from the
Expected behaviorIt is expected to be able to catch any exception that being thrown and handle them necessarily instead of breaking the iteration. Actual behaviorExceptions thrown from Regression?No response Known WorkaroundsThe workaround is manually using IEnumerator.Movenext() method and IEnumerator.Current property.
ConfigurationThis behavior was seen in .NET 6, 7 and 8 Other informationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsDescriptionWhen you iterate through a collection using the Reproduction StepsIt is difficult to provide an example because, usually exception causing member is unexpected in the collection. One of the possible sources could be a enumerable returned from the
Expected behaviorIt is expected to be able to catch any exception that being thrown and handle them necessarily instead of breaking the iteration. Actual behaviorExceptions thrown from Regression?No response Known WorkaroundsThe workaround is manually using IEnumerator.Movenext() method and IEnumerator.Current property.
ConfigurationThis behavior was seen in .NET 6, 7 and 8 Other informationNo response
|
Beta Was this translation helpful? Give feedback.
-
I'm not sure whether the enumerator allows re-entrance when |
Beta Was this translation helpful? Give feedback.
-
Given that this is an uncommon thing to want to do, having to rely on manual enumeration rather than I would also expect that most enumerables aren't able to recover from an exception being thrown like this. |
Beta Was this translation helpful? Give feedback.
Given that this is an uncommon thing to want to do, having to rely on manual enumeration rather than
foreach
feels like a sufficient way to address this.I would also expect that most enumerables aren't able to recover from an exception being thrown like this.