【问题标题】:Boost::file_system: Checking error codesBoost::file_system:检查错误代码
【发布时间】:2014-07-13 23:26:52
【问题描述】:

虽然我使用的是 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()的值相同。

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

【问题讨论】:

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


    【解决方案1】:

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

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

    返回: lhs.category() == rhs.category() &amp;&amp; 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);    
      }
    }
    

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

    #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
    

    【讨论】:

      猜你喜欢
      • 2012-03-02
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多