IOException HResult possible values

2

I find out that sometimes in IO operations common IOException is thrown. I can use Marshal.GetHRForException(Exception e) method (found here) in order to determine the specific error code. After that, I want to re-throw an exception, using my application specific error code, in order to give the user some tips what has gone wrong. But I was unable to find the list of possible HResult values for IOException.

One more thing I'm stuck with is that for different HResult codes root cause can be the same - for example, 0x8007052E and 0x80070569 - both correspond to Logon failure: unknown user name or bad password.

I have tried to search something like "IOException HResult values" but there was nothing special for mapping IOException HResult with human-readable error (like this or this).

I have also seen the question here, which is very similar, but there still no solution.

So, where is the documentation about IOException HResults values?

c#
.net
error-handling
ioexception
hresult
asked on Stack Overflow Sep 9, 2014 by stukselbax • edited May 23, 2017 by Community

1 Answer

9

By using Marshal.GetHRForException() you wrapped the Windows error code into an HResult error code. For an IOException it will always be the Windows error number + 0x80070000. The "8" means error, the "7" means it came from Windows. You could simply use & 0xffff to recover the original error code again.

Convert the result back to decimal and you'll have lots of places to find out what the error code means. I personally always use the WinError.h SDK header file. Depends on what version of VS you have, most programmers will find it back in the C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include directory.

You also have a handy utility available, run errlook.exe from the Visual Studio Command Prompt. A good way to have it readily available is by adding it to the Tools menu. Use Tools + External Tools, Add button, Title = "Error lookup", "Command" = C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\errlook.exe. Tweak the path as necessary to match your VS version.

You'll now also find that the error codes are not the same. 0x80070569 & 0xffff = 0x569 = 1385. Searching WinError.h for that error code yields ERROR_LOGON_TYPE_NOT_GRANTED, "Logon failure: the user has not been granted the requested logon type at this computer".

The kind of IOException errors you can get are not in anyway restricted. Windows does not have "exception specifications", only the most common error codes are listed in the MSDN documentation for a particular winapi function. Much like .NET exceptions. A necessary evil, many IOException error codes are generated by drivers that were not written by Microsoft.

answered on Stack Overflow Sep 9, 2014 by Hans Passant • edited Sep 9, 2014 by Hans Passant

User contributions licensed under CC BY-SA 3.0