Boost :: file_system:检查错误代码

Per*_*-lk 5 c++ boost boost-filesystem system-error

虽然我正在使用C++ 11,但这个问题与提升相关,因为我正在处理错误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
}
Run Code Online (Sandbox Code Playgroud)

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

// ...
} catch (filesystem_error& e) {
     cout << "Value: " << e.code().value() << endl;
}
Run Code Online (Sandbox Code Playgroud)

我得到了价值2.它的价值是相同的e.code().default_error_condition().value().

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

Tan*_*ury 9

error_codeserror_conditions不同error_categories的允许具有相同的value().所述非成员比较函数 同时检查值和类别:

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

返回: lhs.category() == rhs.category() && lhs.value() == rhs.value().

因此,error_code可以根据返回来检查异常make_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);    
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个完整的示例,演示了两个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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

其中产生以下输出:

No file or directory
Run Code Online (Sandbox Code Playgroud)