How to Check If Ioexception Is Not-Enough-Disk-Space-Exception Type

How to check if IOException is Not-Enough-Disk-Space-Exception type?

You need to check the HResult and test against ERROR_DISK_FULL (0x70) and ERROR_HANDLE_DISK_FULL (0x27), which can be converted to HResults by OR'ing with 0x80070000.

For .Net Framework 4.5 and above, you can use the Exception.HResult property:

static bool IsDiskFull(Exception ex)
{
const int HR_ERROR_HANDLE_DISK_FULL = unchecked((int)0x80070027);
const int HR_ERROR_DISK_FULL = unchecked((int)0x80070070);

return ex.HResult == HR_ERROR_HANDLE_DISK_FULL
|| ex.HResult == HR_ERROR_DISK_FULL;
}

For older versions, you can use Marshal.GetHRForException to get back the HResult, but this has significant side-effects and is not recommended:

static bool IsDiskFull(Exception ex)
{
const int ERROR_HANDLE_DISK_FULL = 0x27;
const int ERROR_DISK_FULL = 0x70;

int win32ErrorCode = Marshal.GetHRForException(ex) & 0xFFFF;
return win32ErrorCode == ERROR_HANDLE_DISK_FULL || win32ErrorCode == ERROR_DISK_FULL;
}

From the MSDN documentation:

Note that the GetHRForException method sets the IErrorInfo of the
current thread. This can cause unexpected results for methods like the
ThrowExceptionForHR methods that default to using the IErrorInfo of
the current thread if it is set.

See also How do I determine the HResult for a System.IO.IOException?

Does an IO exception get thrown if the disk is full?

You will get an IO exception:

System.IO.IOException: There is not enough space on the disk.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

The System.IO library handles the actual tasks of reading and writing to disk and wraps them in managed code, so you shouldn't get any unmanaged exceptions using them.

It's also worth trying this (with one of those shoddy small USB sticks they give out everywhere) to see what happens with your code - that's usually the best way of finding out this sort of thing.

Java.io.IO.Exception; there is not enough space on the disk while running J2ME application

With respect to your question, as per the given information,

Java.io.IOException; there is not enough space on the disk

is thrown, say if you try copying something to a destination drive, if the drive is full, you get the exception. The Java.io.IOException exceptions produced by failed or interrupted I/O operations, in this case an interupted IO operation and the reason is there is not enough space on the disk , I think its pretty clear from the message.

Disk Space error

Whenever you want to ignore an error, you can wrap the call in a try block and swallow the error like this:

try
{
DoSomethingThatCausesAnError();
}
catch
{
//Swallow the exception
}

If at all possible you should only swallow specific errors:

try
{
DoSomethingThatCausesAnError();
}
catch(System.IO.IOException exception)
{
//Swallow the exception
}

Unfortunately it's a little tricky to catch ONLY "drive full" errors. You need to get access to the protected HResult member. For tips on how to do that, click here

P.S. It's probably a really bad idea to do any of this. You really ought to figure out why your system is reporting disk full errors and take care of the root cause.



Related Topics



Leave a reply



Submit