C#C
C#β€’15mo agoβ€’
73 replies
Ruttie

Try-catch in an iterator

Is it possible to do a try-catch, or alternatively a try-finally over a yield return statement?
When I try something like this:
IEnumerator Method1(IDisposable disp) {
    try {
        yield return Method2();
    }
    finally {
        disp.Dispose();
    }
}
then the Dispose method is never called.
Was this page helpful?