Mastering Error Handling with std::error_condition in C++

2025-09-07

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.


cpp



C++ alignas: A Developer's Guide to Memory Alignment

The alignas specifier is a part of the C++ standard that allows you to control the memory alignment of a variable or a type


Mastering C++ I/O: From fgets to getline

std::fgets is a C-style function often used for reading strings from a file or stdin. While it can be useful, C++ developers often encounter a few common pitfalls


Bridging C and C++: An Introduction to std::out_ptr_t

std::out_ptr_t is a C++ smart pointer utility introduced in C++23. Its main purpose is to help you safely and easily manage the lifetime of a raw pointer that's being assigned to by a function



Mastering std::unique_lock::operator=: Move Semantics in C++

A common mistake is trying to copy a std::unique_lock instead of moving it. Because std::unique_lock objects manage a mutex


C++ Ranges: Managing Lifetime and Iteration with zip_transform_view

The function std::ranges::zip_transform_view::begin() is the method that returns an iterator pointing to the first element of the combined


A Friendly Guide to std::less in C++

In simple terms, std::less<void> is a special function object (a class that acts like a function) that lets you perform a less-than comparison (a < b) without having to specify the exact data types upfront


C++ std::piecewise_construct Explained: When to Use and How to Avoid Pitfalls

First off, let's understand what std::piecewise_construct is for. It's not a function or an object you'd use on its own


Don't Dereference the End: Common Mistakes with std::basic_string::cend and How to Fix Them

Hey there! Let's dive into some of the common issues with std::basic_string::cend in C++ and explore some great alternatives


Beyond std::fstream: A Deep Dive into std::basic_filebuf Swapping

Hey there! Let's talk about std::swap(std::basic_filebuf). This function is part of the C++ standard library and is designed to efficiently exchange the contents of two std::basic_filebuf objects


Why You Should Prefer Over std::FILE for C++ I/O

When working with std::FILE in C++, which uses functions like fopen, fread, fwrite, and fclose, you'll often run into issues related to safety