system_error


1. Catching a system error using try and catch

#include <iostream>
#include <system_error>

int main() {
    try {
        // Code that may throw a system error
    } catch (std::system_error& e) {
        std::cerr << "Error code: " << e.code() << '\n';
        std::cerr << "Message: " << e.what() << '\n';
    }

    return 0;
}

2. Getting the error code and message

std::error_code ec;
try {
   // Code that may throw a system error
} catch(...) {
   // The default constructor for std::error_code creates
   // an error code with value 0 and message "Success".
   ec = std::error_code(std::current_exception());
}

std::cout << "Error code: " << ec.value() << '\n';
std::cout << "Message: " << ec.message() << '\n';

3. Checking for a specific error code

4. Using std::make_error_code to create an error code

5. Using std::error_category to get a human-readable description of the error

6. Using std::system_category to get the error category for system errors

7. Using std::generic_category to get the error category for generic errors

8. Using std::io_category to get the error category for I/O errors

9. Using std::network_category to get the error category for network errors

10. Using std::system_error to create a system error

11. Using std::make_system_error to create a system error

12. Using std::error_condition to create an error condition

13. Using std::make_error_condition to create an error condition