The std::system_error
example above is slightly incorrect. std::system_category()
will map the error codes from system's native error code facility. For *nix, this is errno
. For Win32, it is GetLastError()
. ie, on Windows, the above example will print
failed to open C:\path\to\forbidden: The data is invalid
because EACCES is 13 which is the Win32 error code ERROR_INVALID_DATA
To fix it, either use the system's native error code facility, eg on Win32
throw new std::system_error(GetLastError(), std::system_category(), "failed to open"+ filename);
Or use errno and std::generic_category()
, eg
throw new std::system_error(errno, std::generic_category(), "failed to open"+ filename);