Site Tools


wiki:hpp-system-error

Table of Contents

<system_error>

<system_error> provides std::error_code and std::error_category for OS-level error reporting. Use it to portably capture and classify system errors (like errno or Windows HRESULT) without hardcoding values.

Most code doesn't use this directly; it's used internally by file operations and network APIs.

Example

This example captures an error when opening a nonexistent file and describes it using the generic error category.

// compile: g++ -std=c++11 -o systemerrorexample systemerrorexample.cpp
// run: ./systemerrorexample
// description: system error handling
 
#include <system_error>
#include <fstream>
#include <iostream>
 
int main() {
    std::ifstream file("nonexistent.txt");
    if (!file) {
        std::error_code ec(errno, std::generic_category());
        std::cout << "error: " << ec.message() << "\n";
    }
 
    return 0;
}
wiki/hpp-system-error.md · Last modified: by 127.0.0.1