Boost :: file_system:检查错误代码 [英] Boost::file_system: Checking error codes

查看:441
本文介绍了Boost :: file_system:检查错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Although I'm using C++11, this question is boost-related, since I'm processing errors from 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)时的错误值:

If I print the error value when the desired exception (no_such_file_or_directory) is thrown:

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

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

I get the value 2. It is the same value of e.code().default_error_condition().value().

是:可能不同的错误条件从不同的错误类别有相同的值吗?我的意思是,我需要检查两个,错误类别和错误值,以确保我得到一个特定的错误?在这种情况下,最简单的方法是什么?

My questions is: could different error conditions from different error categories have same values? I mean, does I need to check both, error categories and error values, in order to ensure I'm getting a specific error? In such a case, what is the cleanest way to do it?

推荐答案

error_codes error_conditions error_categories 允许具有相同的 value()非成员比较函数检查两者值和类别:

error_codes and error_conditions with different error_categories are allowed to have the same value(). The non-member comparison functions check both the the value and category:


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

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

可以根据 make_error_code()的返回值检查$ c> error_code ,如下所示:

Hence, the exceptions's error_code could be checked against the return from make_error_code(), such as follows:

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);    
  }
}






这是一个完整的示例,演示两个 error_code 不等价,尽管具有相同的值:


Here is a complete example demonstrating two error_codes that are not equivalent despite having the same value:

#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

这篇关于Boost :: file_system:检查错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆