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 6679977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:25:17+00:00 2026-05-26T04:25:17+00:00

I have the following function which reads from a firebird database. The Function works

  • 0

I have the following function which reads from a firebird database. The Function works but does not handle exceptions (Required).

public IEnumerable<DbDataRecord> ExecuteQuery(string Query)
{
    var FBC = new FbCommand(Query, DBConnection);

    using (FbDataReader DBReader = FBC.ExecuteReader())
    {
        foreach (DbDataRecord record in DBReader)
            yield return record;
    }
}

Adding try/catch to this function gives an error regarding yield. I understand why I get the error but any workround I’ve tried has resulted in DBReader being disposed indirectly via using() too early or Dispose() not being called all. How do I get this code to use Exceptions & Cleanup without having to wrap the method or duplicate DBReader which might contain several thousand record?

Update:

Here is an example of an attempted fix. In this case DBReader is being disposed too early.

    public IEnumerable<DbDataRecord> ExecuteQuery(string Query)
    {   
        var FBC = new FbCommand(Query, DBConnection);

        FbDataReader DBReader = null;

        try
        {
            using (DBReader = FBC.ExecuteReader());
        }
        catch (Exception e)
        {
            Log.ErrorException("Database Execute Reader Exception", e);
            throw;
        }

        foreach (DbDataRecord record in DBReader) <<- DBReader is closed at this stage
            yield return record;

    }
  • 1 1 Answer
  • 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. Editorial Team
    Editorial Team
    2026-05-26T04:25:17+00:00Added an answer on May 26, 2026 at 4:25 am

    The code you’ve got looks fine to me (except I’d use braces round the yield return as well, and change the variable names to fit in with .NET naming conventions 🙂

    The Dispose method will only be called on the reader if:

    • Accessing MoveNext() or Current in the reader throws an exception
    • The code using the iterator calls dispose on it

    Note that a foreach statement calls Dispose on the iterator automatically, so if you wrote:

    foreach (DbDataRecord record in ExecuteQuery())
    {
        if (someCondition)
        {
            break;
        }
    }
    

    then that will call Dispose on the iterator at the end of the block, which will then call Dispose on the FbDataReader. In other words, it should all be working as intended.

    If you need to add exception handling within the method, you would need to do something like:

    using (FbDataReader DBReader = FBC.ExecuteReader())
    {
        using (var iterator = DBReader.GetEnumerator())
        {
            while (true)
            {
                DbDataRecord record = null;
                try
                {
                    if (!iterator.MoveNext())
                    {
                        break;
                    }
                    record = iterator.Current;
                }
                catch (FbException e)
                {
                    // Handle however you want to handle it
                }
                yield return record;
            }
        }
    }
    

    Personally I’d handle the exception at the higher level though…

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

Sidebar

Related Questions

I have the following JavaScript code: Link In which the function makewindows does not
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
So: I have the following function, adapted from a formula found online, which takes
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I have the following script which works kind of... $(document).ready(function(){ // add or remove
Let's say I want to have a function which reads data from the SerialPort
I have the following code , which gets a return value from a function
I have the following jquery which reads from the excellent geonames.org web service: $('#<%=txtFindMyCity.ClientID%>').autocomplete(http://ws.geonames.org/searchJSON,
I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
I have the following C++ function definition, which I am trying to call through

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.