I need to do some heavy, somewhat fragile logic in a method that I'm implementing as an iterator (using yield
):
public IEnumerable<Things> GetMoreThings() {
while (goodStuffHappens()) {
Things moreThingsIWant = TemptFateAgain();
if (moreThingsIWant.Any())
yield return moreThingsIWant;
}
}
Out in the calling method, I need to wrap the call to GetMoreThings
in try
/catch
and yield return
the result:
try {
foreach (Things thing in Helpful.GetMoreThings())
yield return thing;
}
catch (Exception e) {
//crash, burn
}
The initiated will immediately realize that this is impossible - there is no such thing as a yield inside a try
/catch
block (only try
/finally
).
Any recommendations?