Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update</strong></p> <p>This will be supported in C# 6. It turned out that it wasn't fundamentally impossible, and the team worked out how to do so without going mad in the implementation :)</p> <p><strong>Original answer</strong></p> <p>I strongly suspect it's the same <a href="http://blogs.msdn.com/b/ericlippert/archive/2009/07/20/iterator-blocks-part-four-why-no-yield-in-catch.aspx" rel="nofollow noreferrer">reasoning that prevents <code>yield return</code> from being used in a catch block</a>.</p> <p>In particular:</p> <blockquote> <p>First off, we still have the problem that it is illegal to "goto" into the middle of the handler of a try-protected region. The only way to enter a catch block is via the "non-local goto" that is catching an exception. So once you yielded out of the catch block, the next time MoveNext was called, we’d have no way to get back into the catch block where we left off.</p> <p>Second, the exception that was thrown and caught is an intrinsic part of the execution of the catch block because it can be re-thrown using the "empty throw" syntax. We have no way of preserving that state across calls to MoveNext.</p> </blockquote> <p>Replace "yield" with "await" there, and I think you'll have your answer.</p> <p>It feels like it would be an odd thing to <em>want</em> to do in most cases, and you should <em>usually</em> be able to rewrite your code fairly easily to await after the <code>catch</code> block - unless you were trying to await something and then throw, of course. In that case it would be a bit of a pain, I admit...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload