【发布时间】:2016-11-09 04:46:31
【问题描述】:
我正在考虑从调用 Windows 切换到使用 boost::filesystem。但是,文档实际上几乎没有告诉我如何从中获取有意义的错误信息。
举个简单的例子,我做了以下操作
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 的值。 如果我在 windows 标题中查找 123,它会将我指向 ENOPROTOOPT 的本机错误和 no_protocol_option 的 boost 错误。这不可能。
该消息有些有用,并显示“文件名、目录名称或卷标语法不正确”但是,我不确定我是否应该依赖始终填写的消息或是否有意义。对于这种情况可能会好很多,并且 switch 语句 + 手动消息似乎很合适。
从 boost::filesystem 中获取有意义的错误信息的正确方法是什么?有意义的是可以查找和比较的字符串消息和错误代码。
编辑: 我还发现一些较早的论坛帖子和文章提到了 native_error(),但是,在我的调试器中,版本 1.62 中的异常似乎没有暴露任何此类方法。
我找到的相关链接: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#Error-reporting
【问题讨论】:
标签: c++ boost error-handling