Although I'm using C++11, this question is boost-related, since I'm processing 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 (no_such_file_or_directory) is thrown:
// ...
} catch (filesystem_error& e) {
cout << "Value: " << e.code().value() << endl;
}
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
anderror_conditions
with differenterror_categories
are allowed to have the samevalue()
. The non-member comparison functions check both the the value and category:Hence, the exceptions's
error_code
could be checked against the return frommake_error_code()
, such as follows:Here is a complete example demonstrating two
error_code
s that are not equivalent despite having the same value:Which produces the following output: