<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx</link><description>There are three scenarios in which code could be executing in a finally in an iterator block. In none of them is it a good idea to yield a value from inside the finally, so this is illegal across the board. The three scenarios are (1) normal cleanup,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9835771</link><pubDate>Thu, 16 Jul 2009 18:43:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9835771</guid><dc:creator>Matthew Jones</dc:creator><description>&lt;P&gt;Whoa, what an amazing post! &amp;nbsp;This kind of stuff really opens your eyes to the kind of problems you guys encounter when designing features. &amp;nbsp;The scenarios you describe in your post sound like a real nightmare.&lt;/P&gt;
&lt;P&gt;I'd love to be a fly on the wall when you guys discuss these things in your design meetings. &amp;nbsp;A podcast would be cool ;-)&lt;/P&gt;
&lt;P&gt;There's a point in the post where you mention having to convince the CLR guys to allow spaghetti code. &amp;nbsp;Do you run into situations where you have new requirements of the CLR often? &amp;nbsp;Or is this one of the many signs that a feature should be cut?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;We try to not drive features into the CLR, though it does happen occasionally. For example, the "Silverlight" CLR and the "desktop" CLR have slightly different security models for generated code, which makes dynamic code spitting via compilation of expression trees tricky in Silverlight; that language feature drove a change into the Silverlight CLR that was not anticipated in the design. Or, as we expose existing CLR features like generic variance, suddenly features that have been not heavily tested historically are suddenly in more widespread usage, which can trigger CLR changes to fix design or implementation warts. And sometimes a feature clearly requires CLR changes, like the "no PIA" feature. &lt;/P&gt;
&lt;P&gt;And its a two-way street. Sometimes the CLR guys come up with a great new CLR feature and then ask us if we'd like to provide a "surface" for that feature in our language.&amp;nbsp;-- Eric&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Thanks for the excellent series of posts Eric, I look forward to the rest.&lt;/P&gt;</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9836030</link><pubDate>Thu, 16 Jul 2009 23:09:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9836030</guid><dc:creator>Abhijeet Patel</dc:creator><description>&lt;p&gt;Wow, I never really paid attention to the &amp;quot;why not&amp;quot; for yield returns in iterator blocks, typically all you find in books are a straight copy/paste from the language spec/MSDN about where a construct cannot be used legally. &lt;/p&gt;
&lt;p&gt;# An iterator member's signature cannot contain any out or ref parameters.&lt;/p&gt;
&lt;p&gt;# An iterator cannot contain a return statement .&lt;/p&gt;
&lt;p&gt;# An iterator may not contain any unsafe code.&lt;/p&gt;
&lt;p&gt;# A finally block may not contain a yield return or a yield break statement.&lt;/p&gt;
&lt;p&gt;# A yield return statement may not appear in a try block that has a catch block.&lt;/p&gt;
&lt;p&gt;# A yield return statement may not appear in a catch block (yield break is OK)&lt;/p&gt;
&lt;p&gt;Thanks for providing a very meaningful explanation about the &amp;quot;why&amp;quot; and &amp;quot;why not&amp;quot;. &lt;/p&gt;
&lt;p&gt;Just a side note, most of the material in your blog is something one can never find in books,it's always insightful and stimulating reading your stuff. I always end up walking away with an &amp;quot;a-aha&amp;quot;. :)&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9836184</link><pubDate>Fri, 17 Jul 2009 01:18:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9836184</guid><dc:creator>omario</dc:creator><description>&lt;P&gt;Only "A yield return statement may not appear in a try block that has a catch block" left unexplaned.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;That's part five. Be patient. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9836397</link><pubDate>Fri, 17 Jul 2009 05:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9836397</guid><dc:creator>configurator</dc:creator><description>
&lt;p&gt;I'd go with "A finally block may not contain a yield return or a yield break statement because it makes no sense whatsoever", but I suppose your explanation is a bit more verbose :)&lt;/p&gt;

&lt;p&gt;Seriously though, can you think of an example that makes sense where someone would actually want to do that?&lt;/p&gt;

&lt;p&gt;Also, it seems obvious to me that it wouldn't be possible, since a yield return or break is basically a return, and that is not possible in a finally block.&lt;/p&gt;
&lt;div class="yellowbox"&gt;

&lt;p&gt;Well, hold on a minute before you dismiss it. It only fails to make sense &lt;i&gt;if &lt;/i&gt;you conceive of a yield return/break as a return. The generalized feature's semantics do not &lt;b&gt;obligate &lt;/b&gt;us to conceive of yields in that manner. &lt;/p&gt;&lt;p&gt;The language spec allows us to conceive of yields in that manner, and allows an implementation in which yields actually are returns of the MoveNext method, but as I said in the first couple posts, we could have designed iterators completely differently. &lt;/p&gt;&lt;p&gt;&amp;nbsp;If iterators are coroutines implemented by fibers then a yield in a finally makes perfect sense; a yield &lt;i&gt;anywhere &lt;/i&gt;makes perfect sense. (Well, in a lock is probably a bad idea...) It just means "stop running this fiber and switch back to the main fiber". The fiber has its own stack, so there's no ambiguity about where the exception is going to go; up the stack of the fiber. &lt;/p&gt;&lt;p&gt;Similarly, in a couple of episodes we'll see that if we'd conceived of iterators as "pushing data" instead data of being pulled, then a "yield" isn't logically a &lt;i&gt;return&lt;/i&gt;, it is logically a&lt;i&gt; function call&lt;/i&gt;. And there's no problem at all with function calls in a finally. &lt;/p&gt;&lt;p&gt;Or, if we choose to implement iterators as continuations, then we'd probably be in a system where exception handling is also structured out of continuations; again, it makes sense. It just becomes a new choice about what the structure of the next continuation looks like. &lt;br&gt;&lt;/p&gt;&lt;p&gt;There are lots of ways to conceive of iterators, and the way we chose is actually pretty goofy. By choosing to have an easier time implementing the feature, we ended up in a world where you have to have crazy restrictions on where yields can appear. &lt;br&gt;&lt;/p&gt;&lt;p&gt;-- Eric&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/div&gt;</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9837286</link><pubDate>Fri, 17 Jul 2009 14:49:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9837286</guid><dc:creator>fred</dc:creator><description>&lt;p&gt;// BUG?&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// While examining yield in a try block that has a catch block,&lt;/p&gt;
&lt;p&gt;// I found that generated iterator code leaks implementation detail, namely:&lt;/p&gt;
&lt;p&gt;// &amp;nbsp; 1) If iterator has finally block, Dispose() resets current pointer.&lt;/p&gt;
&lt;p&gt;// &amp;nbsp; 2) If iterator has not finally block, Dispose() does not reset current pointer.&lt;/p&gt;
&lt;p&gt;// I.e., call site can reflect &amp;quot;Does iterator block uses finally block?&amp;quot;.&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// Method TestIterator() intentionally does not use foreach&lt;/p&gt;
&lt;p&gt;// and does not use break or continue, but it is still perfectly legal.&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// My expectation was, that Dispose() will reset current pointer in any case.&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// Tested with 3.5.30729.1 and 4.0.20506.1 C# compilers.&lt;/p&gt;
&lt;p&gt;// csc -target:exe -out:TestDisposeIterator.exe -codepage:1257 Program.cs&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Collections.Generic;&lt;/p&gt;
&lt;p&gt;namespace TestDisposeIterator&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Program&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int sentry = 2;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Program.TestIterator(Program.IteratorWithFinally, sentry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Program.TestIterator(Program.IteratorWithoutFinally, sentry);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static void TestIterator&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; iterator, T sentry)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;where T : struct, IEquatable&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;TestIterator: {0}&amp;quot;, iterator);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (IEnumerator&amp;lt;T&amp;gt; mover = iterator.GetEnumerator())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (mover.MoveNext())&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Current: {0}&amp;quot;, mover.Current);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (mover.Current.Equals(sentry))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mover.Dispose(); &amp;nbsp;// Instead of break.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static IEnumerable&amp;lt;int&amp;gt; IteratorWithFinally&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 2;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 3;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;finally&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static IEnumerable&amp;lt;int&amp;gt; IteratorWithoutFinally&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 2;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return 3;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;// TEST RESULTS:&lt;/p&gt;
&lt;p&gt;//&lt;/p&gt;
&lt;p&gt;// TestIterator: TestDisposeIterator.Program+&amp;lt;get_IteratorWithFinally&amp;gt;d__0&lt;/p&gt;
&lt;p&gt;// Current: 1&lt;/p&gt;
&lt;p&gt;// Current: 2&lt;/p&gt;
&lt;p&gt;// TestIterator: TestDisposeIterator.Program+&amp;lt;get_IteratorWithoutFinally&amp;gt;d__4&lt;/p&gt;
&lt;p&gt;// Current: 1&lt;/p&gt;
&lt;p&gt;// Current: 2&lt;/p&gt;
&lt;p&gt;// Current: 3&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9837490</link><pubDate>Fri, 17 Jul 2009 17:54:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9837490</guid><dc:creator>TheCPUWizard</dc:creator><description>&lt;p&gt;I think we should all yield to this explanation, and finally return to other aspect of the patters involving iterators.... &amp;lt;duck &amp;amp; run&amp;gt;&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9840681</link><pubDate>Mon, 20 Jul 2009 05:03:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9840681</guid><dc:creator>configurator</dc:creator><description>&lt;p&gt;&amp;quot;Well, hold on a minute before you dismiss it.&amp;quot; - I didn't dismiss it! You did, after much forethought. I just gave my first-minute impression of this restriction.&lt;/p&gt;
&lt;p&gt;I never really understood continuations.&lt;/p&gt;
&lt;p&gt;I mean, I know exactly how they work. I've worked with and understood CPS style programing for a while. But I never mastered it, I haven't (yet, hopefully) had that &amp;quot;aha&amp;quot; moment. Maybe for that reason, even if yield were done using continuations, I'd still think of them as a return from a function. So it would still seem obvious to me that it shouldn't work - even if it were an implementation detail of an implementation that doesn't exist!&lt;/p&gt;
&lt;p&gt;That said, I'm still having a hard time coming up with a real-world enough example where someone would want to yield in a finally, and the code would still make sense.&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9842285</link><pubDate>Mon, 20 Jul 2009 23:51:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9842285</guid><dc:creator>Scott Kuhn</dc:creator><description>&lt;p&gt;It's information like this that leads me back to your blog every week. &amp;nbsp;It's one thing to understand a language and a whole other thing to understand why the language is designed and works the way it does. &amp;nbsp;Reading and understanding information like this series is providing gives us users of C# a depth of knowledge that improves our coding skills. &amp;nbsp;Thank you!&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9858242</link><pubDate>Wed, 05 Aug 2009 20:38:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9858242</guid><dc:creator>Mark Sowul</dc:creator><description>&lt;p&gt;&amp;quot;Leaving a finally block via &amp;quot;return&amp;quot; is illegal because it is weird. Suppose the try block returns, causing the finally to execute. What happens when the finally block returns? The original return value would be lost and some other return value would be substituted for it. This seems bizarre and bug-prone, so its not legal; you cannot exit a finally via return.&amp;quot;&lt;/p&gt;
&lt;p&gt;This curious perversion is legal in Java, of course.&lt;/p&gt;
&lt;p&gt;try {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return &amp;quot;5&amp;quot;;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;finally {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return &amp;quot;WTF&amp;quot;;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;cf.&lt;/p&gt;
&lt;p&gt;while(true)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; try {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; finally {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;continue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;System.out.println(&amp;quot;Java is awesome&amp;quot;); //needless to say, never executes&lt;/p&gt;
</description></item><item><title>re: Iterator Blocks, Part Three: Why no yield in finally?</title><link>http://blogs.msdn.com/ericlippert/archive/2009/07/16/iterator-blocks-part-three-why-no-yield-in-finally.aspx#9912990</link><pubDate>Mon, 26 Oct 2009 15:55:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9912990</guid><dc:creator>Ben Voigt [C++ MVP]</dc:creator><description>&lt;p&gt;Eric, since you've (in all the various Iterator Blocks posts, this is just the most relevant) explained how a lot of features have messy interactions with yield return and these interactions have been purposefully forbidden by the C# spec, why is yield return allowed inside a lock block? &amp;nbsp;Could you do a post on lock-yield interactions?&lt;/p&gt;
&lt;p&gt;See also &lt;a rel="nofollow" target="_new" href="http://msmvps.com/blogs/jon_skeet/archive/2009/10/23/iterating-atomically.aspx"&gt;http://msmvps.com/blogs/jon_skeet/archive/2009/10/23/iterating-atomically.aspx&lt;/a&gt; and the comments.&lt;/p&gt;
</description></item></channel></rss>