2 years ago

#53528

test-img

Aram Schiffman

C# Weird behavior after error handling in StreamWriter "using" block

I have C# code that collects data from csv files, and writes it out in a certain way to another csv file. The user has an option to sort or not sort (sorting does some other stuff too), based on state of a checkbox called "sortBox." Most of the work happens inside a using block for a StreamWriter. Within the block, there is a step where it writes to a dictionary.

There is a corner case where sorting makes the dictionary think there is a duplicate value, and an error occurs. The corner case is not worth fixing, and since not sorting is not a big deal, I am trying to offer an option when dictionary error occurs for SW to uncheck sortBox, go back to the start of the method, and write out the unsorted data. Note that the output file has one of two names, depending on whether data are sorted or not. I am fine with having both files, the sorted on never getting written to due to the error, and the unsorted one getting written to.

This is a compact version of my lame attempt. The recursive call might be a dumb idea -- and maybe the reason for the odd behavior I will describe, but I do not know what other approach to take.

    void CollectAndStreamStats(string readFolder, string writeFolder)
{
//<stuff>
try
{
      using (StreamWriter csvOut = new StreamWriter(fullPath))
    {
        //<stuff>
        if (sortBox.Checked)
        {
        //<stuff>
            Try
            {
                resultsDictionary.Add(keyString, str);   //THIS IS WHAT I AM ERROR CHECKING
            }
            catch (ArgumentException e)  //OH  NO, CANNOT ADD TO DICTIONARY, UNCHECKING SORT CHECK BOX SHOULD SOLVE IT
            {
            d = MessageBoc.Show(“You’re screwed, do you want to uncheck sort option and analyze again?”,”Sucker”, MessageBoxButtons.YesNo,other options)
                if (d==DialogResult.Yes)
            {
            csvOut.Close();   //CLOSE STREAM CREATED BY STREAMWRITER 
              sortBox.Checked = false;   //UNCHECK THE BOX, SHOULD WORK OK NOW. (CORNER CASE THING).
            CollectAndStreamStats(csvReadFolder, csvWriteFolder);
            }  //RUN THIS SAME METHOD ALL OVER AGAIN, BUT WITH THAT CHECK BOX UNCHECKED. NOTE THAT CSVoUT WILL HAVE A DIFFERENT NAME AND STREAMING WILL BE TO NEW FILE 
        }
    // **Loop for csvOut.WriteLine goes here if sortBox is checked. It happens elsewhere if it is unchecked.**
    } 
}
catch (IOException e)
{      
        MessageBox.Show("Well, I tried ::shrug::");
}

Well, some of it works. It unchecks sortBox -- local variables confirm this, as does UI -- compiles the unsorted data, and creates and writes to the second (unsorted) file. But then -- despite sortBox.checked being false, it enters the "if (sortBox.Checked)" loop, decides that the file name is the original sorted one again, and tries to write to it, only to throw an error saying it cannot write a closed stream.

No luck with online searches. There must be a right way, any thoughts?

Thanks much in advance, Aram

c#

try-catch

streamwriter

0 Answers

Your Answer

Accepted video resources