473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.IOException 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.SaveFile (saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}

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

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

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

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

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

Nov 17 '05 #1
5 7475
> 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?

I think you want to do something like this.
No need to call the GC.Collect()

try {
try {
richTextBox.SaveFile (saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
} finally {
saveFileDialog1.Dispose (); // Dispose the save file dialog
exception or not
}
} catch (System.IO.IOException ex) {
MessageBox.Show ("Error: " + ex.Message + "\n",
"Problem Saving the Results v2", MessageBoxButtons.OK);
}
}

Nov 17 '05 #2

"Karl" <ka*******@insidertech.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

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.IOException 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.SaveFile (saveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}

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

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

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

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

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


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.SaveFile.

Willy.

Nov 17 '05 #3
Thanks Willy, I thought it was something like that occurring. I'm now
going to use the System.Management functions to check if there is room
on the disk before attempting to write.

Thanks

Karl

Nov 17 '05 #4
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

Nov 17 '05 #5

"Michael Nemtsev" <la*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

No, CER have nothing to do with this, please read about what they are and
where they could apply.

Willy.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Robinson | last post by:
In some code we're writing we're making an assumption, and I'd like to confirm that the assumption is valid. Suppose a signal arrives while a file is being written, and the signal handler...
27
by: garyolsen | last post by:
In C++ what kind of unexpected conditions should be handled as exceptions? Besides dividing by 0, bad memory allocation, what're the most popular exceptions? When should not use exception,...
11
by: suzy | last post by:
i am trying to write aspx login system and the login process requires a validity check of username and password. i have been told that raising exception is costly, but i want a custom error...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
11
by: VJ | last post by:
I have these functions in my application. FileInfo.CopyTo DirectoryInfo.Create File.Copy Directory.Create
4
by: Peter Aitken | last post by:
It seems that some classes have their own exception handling built in. For example if I try to use FileStream to open a file that does not exist, my Try...Catch block seems to be ignored and...
7
by: Garth Wells | last post by:
I'm trying to create a DAL and am wondering what's the proper way to handle errors in this Insert method. public string Insert() { Database db = DatabaseFactory.CreateDatabase(); string...
0
by: =?Utf-8?B?UG9sbHkgQW5uYQ==?= | last post by:
Hi, I have previously used EL v 3.1 Exception Handling application block successfully. I thought I would now try to do the same with EL v 4.0. My first experiment was to replace an exception....
1
by: stephen | last post by:
Hi, I am a lil confused about exception handling. I have a main app and i am importing 2 DLL's. one of the DLL's is for connection/datasets etc and the other has logging errors to file.Both...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.