Boost :: file_system: checking error codes

Although I use C ++ 11, this question is related to boost as I handle errors from boost::file_system.

In the following situation:

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
}

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

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

I get the value 2. This is the same meaning e.code().default_error_condition().value().

My questions are: can different error conditions from different error categories have the same meanings? I mean, do I need to check both error categories and error values ​​to make sure I get a specific error? Then what is the cleanest way to do this?

+4
source share
1 answer

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

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
+8

Source: https://habr.com/ru/post/1541911/


All Articles