Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 99467

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:23:35+00:00 2026-05-11T00:23:35+00:00

The following is okay: try { Console.WriteLine("Before"); yield return 1; Console.WriteLine("After"); } finally {

  • 0

The following is okay:

try {     Console.WriteLine("Before");      yield return 1;      Console.WriteLine("After"); } finally {     Console.WriteLine("Done"); } 

The finally block runs when the whole thing has finished executing (IEnumerator<T> supports IDisposable to provide a way to ensure this even when the enumeration is abandoned before it finishes).

But this is not okay:

try {     Console.WriteLine("Before");      yield return 1;  // error CS1626: Cannot yield a value in the body of a try block with a catch clause      Console.WriteLine("After"); } catch (Exception e) {     Console.WriteLine(e.Message); } 

Suppose (for the sake of argument) that an exception is thrown by one or other of the WriteLine calls inside the try block. What’s the problem with continuing the execution in catch block?

Of course, the yield return part is (currently) unable to throw anything, but why should that stop us from having an enclosing try/catch to deal with exceptions thrown before or after a yield return?

Update: There’s an interesting comment from Eric Lippert here – seems that they already have enough problems implementing the try/finally behaviour correctly!

EDIT: The MSDN page on this error is: http://msdn.microsoft.com/en-us/library/cs1x15az.aspx. It doesn’t explain why, though.

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:23:36+00:00Added an answer on May 11, 2026 at 12:23 am

    I suspect this is a matter of practicality rather than feasibility. I suspect there are very, very few times where this restriction is actually an issue that can’t be worked around – but the added complexity in the compiler would be very significant.

    There are a few things like this that I’ve already encountered:

    • Attributes not being able to be generic
    • Inability for X to derive from X.Y (a nested class in X)
    • Iterator blocks using public fields in the generated classes

    In each of these cases it would be possible to gain a little bit more freedom, at the cost of extra complexity in the compiler. The team made the pragmatic choice, for which I applaud them – I’d rather have a slightly more restrictive language with a 99.9% accurate compiler (yes, there are bugs; I ran into one on SO just the other day) than a more flexible language which couldn’t compile correctly.

    EDIT: Here’s a pseudo-proof of how it why it’s feasible.

    Consider that:

    • You can make sure that the yield return part itself doesn’t throw an exception (precalculate the value, and then you’re just setting a field and returning ‘true’)
    • You’re allowed try/catch which doesn’t use yield return in an iterator block.
    • All local variables in the iterator block are instance variables in the generated type, so you can freely move code to new methods

    Now transform:

    try {     Console.WriteLine('a');     yield return 10;     Console.WriteLine('b'); } catch (Something e) {     Console.WriteLine('Catch block'); } Console.WriteLine('Post'); 

    into (sort of pseudo-code):

    case just_before_try_state:     try     {         Console.WriteLine('a');     }     catch (Something e)     {         CatchBlock();         goto case post;     }     __current = 10;     return true;  case just_after_yield_return:     try     {         Console.WriteLine('b');     }     catch (Something e)     {         CatchBlock();     }     goto case post;  case post;     Console.WriteLine('Post');   void CatchBlock() {     Console.WriteLine('Catch block'); } 

    The only duplication is in setting up try/catch blocks – but that’s something the compiler can certainly do.

    I may well have missed something here – if so, please let me know!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Steve Sanderson blogged about using partial requests in ASP.NET MVC,… May 11, 2026 at 12:01 pm
  • added an answer Try: GEvent.addListener(map, 'maptypechanged', function() { var newMapType = map.getCurrentMapType(); //… May 11, 2026 at 12:01 pm
  • added an answer APC — standard choice, included in PECL, comes prepackaged in… May 11, 2026 at 12:01 pm

Related Questions

The following is okay: try { Console.WriteLine("Before"); yield return 1; Console.WriteLine("After"); } finally {
The following is a Simple Style for ListBoxItem, ListBoxItem has a son Border. Border
Disclaimer: the following is a sin against XML. That's why I'm trying to change
I know that the following is true int i = 17; //binary 10001 int
Which of the following is better code in c# and why? ((DateTime)g[0][MyUntypedDateField]).ToShortDateString() or DateTime.Parse(g[0][MyUntypedDateField].ToString()).ToShortDateString()
any idea how if the following is possible in PHP as a single line
(All of the following is to be written in Java) I have to build
Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference
Just wondering what the difference between MFC control messages prefixed with the following is:
I guess, the following is a standard problem on every school or university: It

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.