【问题标题】:How to get the meaning of error codes from boost::filesystem::filesystem_error如何从 boost::filesystem::filesystem_error 获取错误代码的含义
【发布时间】: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

catching exception from boost::filesystem::is_directory

【问题讨论】:

标签: c++ boost error-handling


【解决方案1】:

WinError.h 是这样说的:

//
// MessageId: ERROR_INVALID_NAME
//
// MessageText:
//
// The filename, directory name, or volume label syntax is incorrect.
//
#define ERROR_INVALID_NAME               123L    // dderror

使用errorCode.message(); 作为引号,您总是会得到可读的错误描述。

【讨论】:

  • 嗯,所以我们认为它们都与msdn.microsoft.com/en-us/library/windows/desktop/… 相关并且总是有一个字符串?我仍然想制作自己的消息并比较开关或其他东西中的值。即使是示例中的那个也不是最好的。如果是这样的话,那将起作用。我会做几个测试。谢谢。
  • 99.9% 的系统错误代码具有相关的字符串值。此外 - 在翻译的 Windows 界面上,它已经翻译了消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 2018-07-28
相关资源
最近更新 更多