Is it possible to just silently terminate an IAsyncEnumerable<T>
iterator on exception?
For example, my naive approach was as follows:
public async IAsyncEnumerable<T2> Map<T1, T2>(IAsyncEnumerable<T1> source, Func<T1, T2> map)
{
try
{
await foreach(var elem in source)
yield return map(elem);
}
catch
{
yield break;
}
}
This, however, does not work, because yield return
is not allowed inside a try/catch
block. The issue is tracked in dotnet repository, but no fix is proposed so far.
Is there any way to rewrite it without having to forgo compiler-generated iterator?
Note: I would like to catch all exceptions, including ones from underlying iterator (e.g. TaskCancelledException
from a cancellation token).