Mastering Error Handling with std::error_condition in C++
Think of std::error_condition as a portable, generic error code. While std::error_code is tied to a specific platform or system (like a errno value on POSIX systems or a Win32 error code), std::error_condition is an abstraction. It represents the meaning of an error rather than the specific, underlying value.
For example, on different operating systems, the error for "permission denied" might be represented by different integer values. std::error_condition allows you to check for the concept of "permission denied" without worrying about the underlying system-specific number. This is super useful for writing cross-platform code.
This is the most common point of confusion. People often try to directly compare an std::error_code to an std::error_condition, which can lead to unexpected results.
The Problem
#include <iostream>
#include <system_error>
int main() {
std::error_code ec; // Let's pretend this is set to a system error.
// This might seem logical, but it's often wrong.
if (ec == std::error_condition(std::io_errc::stream)) {
std::cout << "The error code matches the condition." << std::endl;
} else {
std::cout << "The error code does not match the condition." << std::endl;
}
return 0;
}
In this example, the direct comparison ec == std::error_condition(...) won't work as you'd expect unless a specific mapping has been defined. std::error_code and std::error_condition are different types and represent different concepts.
The Solution
The correct way to compare is to check if the std::error_code matches the std::error_condition.
#include <iostream>
#include <system_error>
int main() {
// This is a pretend error code, but it represents "permission denied"
// on a POSIX-like system where EACCES is 13.
std::error_code ec(13, std::system_category());
// The condition we want to check for is "permission denied"
std::error_condition cond = std::make_error_condition(std::errc::permission_denied);
if (ec == cond) {
std::cout << "The error code matches the 'permission denied' condition!" << std::endl;
} else {
std::cout << "The error code does not match." << std::endl;
}
return 0;
}
The ec == cond comparison is a special overload that calls the std::error_code's matches function. This function checks if the std::error_code's category (in this case, std::system_category) knows how to map its value to the given std::error_condition. This is the right way to achieve cross-platform error handling.
If you're creating a library and want to use std::error_code and std::error_condition, you might need to create your own custom error category. A common mistake is to forget to tell the system how your custom error codes map to standard error conditions.
The Problem
#include <iostream>
#include <system_error>
// A custom error category that doesn't define a mapping
class MyCustomCategory : public std::error_category {
public:
const char* name() const noexcept override { return "MyCustomCategory"; }
std::string message(int ev) const override { return "Some custom error message."; }
};
const MyCustomCategory& my_custom_category() {
static MyCustomCategory instance;
return instance;
}
int main() {
// My custom error code
std::error_code ec(1, my_custom_category());
// This comparison will fail because no mapping is defined.
if (ec == std::make_error_condition(std::errc::bad_address)) {
std::cout << "This won't be printed." << std::endl;
}
return 0;
}
The comparison will always be false because my_custom_category() doesn't have a way to translate its internal error value (1) into a standard std::error_condition.
The Solution
You must override the std::error_category::equivalent function to provide the mapping logic.
#include <iostream>
#include <system_error>
// A custom error category that correctly defines a mapping
class MyCustomCategory : public std::error_category {
public:
const char* name() const noexcept override { return "MyCustomCategory"; }
std::string message(int ev) const override { return "Some custom error message."; }
// This is the key! We define how our custom errors map to standard conditions.
bool equivalent(int code, const std::error_condition& condition) const noexcept override {
// Let's say our internal error code '1' maps to 'permission_denied'
if (code == 1 && condition == std::errc::permission_denied) {
return true;
}
return false;
}
};
const MyCustomCategory& my_custom_category() {
static MyCustomCategory instance;
return instance;
}
int main() {
// My custom error code
std::error_code ec(1, my_custom_category());
// Now, this comparison will work as expected!
if (ec == std::make_error_condition(std::errc::permission_denied)) {
std::cout << "The error code matches the 'permission denied' condition!" << std::endl;
} else {
std::cout << "The error code does not match." << std::endl;
}
return 0;
}
By overriding equivalent, you're essentially saying, "When you see my custom error code 1, it means the same thing as std::errc::permission_denied." This is how you make your custom error codes work seamlessly with the standard library's error conditions.