<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.
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; }