Problem handling Disk full exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karl

    Problem handling Disk full exception


    Hi,

    I have some code that will save the contents of a Rich Text Box in
    either a Text or Rich Text Format file. The code is using the
    SaveFileDialog and is working correctly.

    I have been testing the code and added in some exception handling to
    cater for any problems. During testing I have found that if I attempt
    to save to a floppy disc that is full, a System.IO.IOExc eption is raied
    with the message "There is not enough space on the disk". I am catching
    this exception to display a message to the user. I then Dispose of the
    SaveFileDialog. I then perform Garbage Collection via GC.Collect and at
    this point the exception is raised again but is not caught. Even if a
    put a try around the GC.Collect and have a subsequent Catch the
    exception is now handled.

    Can someone explain what I'm doing wrong? I'm guessing that the
    SaveFileDialog is retrying the save and I need to somehow flush it or
    abort?

    The code I'm using :

    try
    {
    richTextBox.Sav eFile (saveFileDialog 1.FileName,
    RichTextBoxStre amType.RichText );
    }

    catch (System.IO.IOEx ception ex)
    {
    MessageBox.Show ("Error: " + ex.Message + "\n",
    "Problem Saving the Results", MessageBoxButto ns.OK);
    }

    saveFileDialog1 .Dispose (); // Dispose of the save file dialog

    try
    {
    GC.Collect (); // collect the garbage
    }

    catch (System.IO.IOEx ception ex)
    {
    MessageBox.Show ("Error: " + ex.Message + "\n",
    "Problem Saving the Results v2", MessageBoxButto ns.OK);
    }

    catch ( Exception ex )
    {
    MessageBox.Show ("Error: " + ex.Message + "\n",
    "Problem Saving the Results v3", MessageBoxButto ns.OK);
    }

  • Olaf Baeyens

    #2
    Re: Problem handling Disk full exception

    > Can someone explain what I'm doing wrong? I'm guessing that the[color=blue]
    > SaveFileDialog is retrying the save and I need to somehow flush it or
    > abort?
    >[/color]
    I think you want to do something like this.
    No need to call the GC.Collect()

    try {
    try {
    richTextBox.Sav eFile (saveFileDialog 1.FileName,
    RichTextBoxStre amType.RichText );
    } finally {
    saveFileDialog1 .Dispose (); // Dispose the save file dialog
    exception or not
    }
    } catch (System.IO.IOEx ception ex) {
    MessageBox.Show ("Error: " + ex.Message + "\n",
    "Problem Saving the Results v2", MessageBoxButto ns.OK);
    }
    }



    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Problem handling Disk full exception


      "Karl" <karl.todd@insi dertech.co.uk> wrote in message
      news:1130928881 .091343.167190@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      >
      > Hi,
      >
      > I have some code that will save the contents of a Rich Text Box in
      > either a Text or Rich Text Format file. The code is using the
      > SaveFileDialog and is working correctly.
      >
      > I have been testing the code and added in some exception handling to
      > cater for any problems. During testing I have found that if I attempt
      > to save to a floppy disc that is full, a System.IO.IOExc eption is raied
      > with the message "There is not enough space on the disk". I am catching
      > this exception to display a message to the user. I then Dispose of the
      > SaveFileDialog. I then perform Garbage Collection via GC.Collect and at
      > this point the exception is raised again but is not caught. Even if a
      > put a try around the GC.Collect and have a subsequent Catch the
      > exception is now handled.
      >
      > Can someone explain what I'm doing wrong? I'm guessing that the
      > SaveFileDialog is retrying the save and I need to somehow flush it or
      > abort?
      >
      > The code I'm using :
      >
      > try
      > {
      > richTextBox.Sav eFile (saveFileDialog 1.FileName,
      > RichTextBoxStre amType.RichText );
      > }
      >
      > catch (System.IO.IOEx ception ex)
      > {
      > MessageBox.Show ("Error: " + ex.Message + "\n",
      > "Problem Saving the Results", MessageBoxButto ns.OK);
      > }
      >
      > saveFileDialog1 .Dispose (); // Dispose of the save file dialog
      >
      > try
      > {
      > GC.Collect (); // collect the garbage
      > }
      >
      > catch (System.IO.IOEx ception ex)
      > {
      > MessageBox.Show ("Error: " + ex.Message + "\n",
      > "Problem Saving the Results v2", MessageBoxButto ns.OK);
      > }
      >
      > catch ( Exception ex )
      > {
      > MessageBox.Show ("Error: " + ex.Message + "\n",
      > "Problem Saving the Results v3", MessageBoxButto ns.OK);
      > }
      >[/color]

      The problem is that whatever you try (Dispose, GC etc...) , the function
      tries to flush the buffer and this is just the problem, it can't because the
      is no space on disk.
      What you could do (if possible) before you start writing to a disk is to
      have a look whether there is anough free space to write your data. If this
      isn't possible, your only option is to close the file using Win32's API
      CloseHandle, only problem is how to get at the underlying file handle when
      using RichTextBox.Sav eFile.

      Willy.



      Comment

      • Karl

        #4
        Re: Problem handling Disk full exception

        Thanks Willy, I thought it was something like that occurring. I'm now
        going to use the System.Manageme nt functions to check if there is room
        on the disk before attempting to write.

        Thanks

        Karl

        Comment

        • Michael Nemtsev

          #5
          Re: Problem handling Disk full exception

          I recomend to look at Constrained Execution Regions. It's FW 2.0
          feature to solve yo problem
          Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


          Comment

          • Willy Denoyette [MVP]

            #6
            Re: Problem handling Disk full exception


            "Michael Nemtsev" <laflour@gmail. com> wrote in message
            news:1131028658 .651352.327450@ g43g2000cwa.goo glegroups.com.. .[color=blue]
            >I recomend to look at Constrained Execution Regions. It's FW 2.0
            > feature to solve yo problem
            > http://msdn.microsoft.com/msdnmag/is...y/default.aspx
            >[/color]
            No, CER have nothing to do with this, please read about what they are and
            where they could apply.

            Willy.


            Comment

            Working...