我正在考虑从调用Windows切换到使用boost :: filesystem。但是,文档确实告诉我几乎没有关于如何从中获取有意义的错误信息。如何从boost :: filesystem :: filesystem_error获取错误代码的含义
对于一个简单的例子,我做了以下
try
{
// Creates all directories in the path if they do not exist
boost::filesystem::create_directories("!?#Gibberish!?#");
}
catch(boost::filesystem::filesystem_error & e)
{
// Not very clear on how to get meaningful information from the exception
// The codes are found in boost::system::errc::<your code here>
// Try and get the value and then find the Windows codes mapped to the boost codes?
// The actual numeric value can be found in the header with the Windows codes - errno.h under _CRT_NO_POSIX_ERROR_CODES?
//
// You'll have to compare against specific ones and make your own meaningful error message?
const boost::system::error_code errorCode = e.code();
std::ostringstream msg;
msg << "boost::filesystem::create_directories failed with error code: " << errorCode.message();
// Use our own exception type
throw Common::Exception(__FILE__, __LINE__, msg.str());
}
e.code()给我在调试器的123值。 如果我在窗口标题中查找123,它会指向ENOPROTOOPT的本地错误和no_protocol_option的升级错误。这是不对的。
该消息有点用处,并说“文件名,目录名称或卷标语法不正确”但是,我不确定我应该依赖于始终填写还是有意义的消息。对于这种情况可能会更好,并且switch语句+手动消息似乎是适当的。
从boost :: filesystem中获取有意义的错误信息的正确方法是什么?有意义的是可以查找和比较的字符串消息和错误代码。我也发现一些较旧的论坛主题和文章提到native_error(),但是,在我的调试器中,版本1.62中的异常似乎没有任何此类方法公开。
相关链接我发现: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#Error-reporting
catching exception from boost::filesystem::is_directory
的可能的复制[升压错误代码人类可读的描述(http://stackoverflow.com/questions/10755084/boost-error-codes-human-readable-description) – Arunmu