c++ - Boost::file_system:检查错误代码

标签 c++ boost boost-filesystem system-error

虽然我使用的是 C++11,但这个问题与 boost 相关,因为我正在处理来自 boost::file_system 的错误。

在以下情况下:

try {

     // If p2 doesn't exists, canonical throws an exception
     // of No_such_file_or_directory
     path p = canonical(p2);

     // Other code

} catch (filesystem_error& e) {

    if (e is the no_such_file_or_directory exception)
        custom_message(e);

} // other catchs
}

如果我在抛出所需的异常 (no_such_file_or_directory) 时打印错误值:

// ...
} catch (filesystem_error& e) {
     cout << "Value: " << e.code().value() << endl;
}

我得到值 2。它与 e.code().default_error_condition().value() 的值相同。

我的问题是:来自不同错误类别的不同错误条件是否可以具有相同的值?我的意思是,我是否需要同时检查错误类别和错误值,以确保收到特定错误?在这种情况下,最干净的方法是什么?

最佳答案

error_codeserror_conditions与不同error_categories允许具有相同的 value()non-member comparison functions检查值和类别:

bool operator==( const error_code & lhs, const error_code & rhs ) noexcept;

Returns: lhs.category() == rhs.category() && lhs.value() == rhs.value().

因此,可以根据 make_error_code() 的返回检查异常的 error_code,如下所示:

try {
  // If p2 doesn't exists, canonical throws an exception
  // of No_such_file_or_directory
  path p = canonical(p2);

  // ...    
} catch (filesystem_error& e) {
  if (e.code() ==
      make_error_code(boost::system::errc::no_such_file_or_directory)) {
    custom_message(e);    
  }
}

这是一个完整的例子demonstrating两个 error_code 尽管具有相同的值但并不等效:

#include <boost/asio/error.hpp>
#include <boost/filesystem.hpp>
#include <boost/system/error_code.hpp>

int main()
{
  // Two different error codes.
  boost::system::error_code code1 = make_error_code(
    boost::system::errc::no_such_file_or_directory);
  boost::system::error_code code2 = make_error_code(
    boost::asio::error::host_not_found_try_again);

  // That have different error categories.
  assert(code1.category() != code2.category());
  assert(code1.default_error_condition().category() !=
         code2.default_error_condition().category());

  // Yet have the same value.
  assert(code1.value() == code2.value());
  assert(code1.default_error_condition().value() ==
         code2.default_error_condition().value());

  // Use the comparision operation to check both value
  // and category.
  assert(code1 != code2);
  assert(code1.default_error_condition() !=
         code2.default_error_condition());

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // Test with Boost.Filesytem
  try
  {
    boost::filesystem::canonical("bogus_file");
  }
  catch(boost::filesystem::filesystem_error& error)
  {
    if (error.code() == 
        make_error_code(boost::system::errc::no_such_file_or_directory))
    {
      std::cout << "No file or directory" << std::endl;
    }
    if (error.code() ==
        make_error_code(boost::asio::error::host_not_found_try_again))
    {
      std::cout << "Host not found" << std::endl;
    }
  }
}

产生以下输出:

No file or directory

关于c++ - Boost::file_system:检查错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23848942/

相关文章:

C++/boost : checking process permission

c++ - Boost.Filesystem 如何找出您的可执行文件在哪个目录中?

c++ - C++11 编译器在代码优化期间是否可以将局部变量转换为右值?

c++ - boost::信号2;没有添加信号槽的匹配函数

c++ - boost 文件系统 directory_iterator 的非确定性执行

c++ - 在 boost 元组 vector 中查找元素的位置

c++ - 在 MSVC 2013 上编译 Boost MPL 示例

C++ 错误 - '.' 标记前的预期主表达式|

c++ - 寻找相似颜色的最佳颜色空间

c++ - RDTSCP 是否跨多核单调递增?