All Error-Code Facilities
All Error-Code Facilities
A compact index of non-throwing error reporting facilities centered on <system_error> and adjacent standard-library types.
All Error-Code Facilities
A compact index of non-throwing error reporting facilities centered on <system_error> and adjacent standard-library types.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
<system_error>std::error_codestd::error_conditionstd::error_categorystd::system_errorstd::errcstd::make_error_codestd::make_error_conditionstd::filesystem::filesystem_errorstd::error_code&error_code out-parameter overloads for non-throwing control flowstd::error_code when local branching is expected and exceptions would be noise.error_category.#include <filesystem>
#include <string_view>
#include <system_error>
std::error_code prepare_output_directory(std::string_view name) {
std::error_code ec;
std::filesystem::path dir{"output"};
dir /= name;
std::filesystem::create_directories(dir, ec);
return ec;
}
int main() {
if (std::error_code ec = prepare_output_directory("reports")) {
return ec == std::errc::permission_denied ? 2 : 1;
}
return 0;
}
This is the common error_code shape: do a non-throwing operation, branch locally, and keep the caller in charge of how much error detail matters.
error_code when failure is expected and the caller can recover immediately#include <filesystem>
#include <system_error>
int main() {
std::error_code ec;
std::filesystem::create_directories("output/cache", ec);
if (ec) {
return ec == std::errc::permission_denied ? 2 : 1;
}
return 0;
}