# system\_error

***

**1. Catching a system error using `try` and `catch`**

```cpp
#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**

```cpp
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**

```cpp
std::error_code ec;
try {
   // Code that may throw a system error
} catch(...) {
   ec = std::error_code(std::current_exception());
}

if (ec == std::errc::invalid_argument) {
   std::cout << "Invalid argument\n";
}
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

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

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::error_category& cat = ec.category();
std::string description = cat.message(ec.value());

std::cout << "Error description: " << description << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::error_category& cat = std::system_category();
std::string description = cat.message(ec.value());

std::cout << "Error description: " << description << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::error_category& cat = std::generic_category();
std::string description = cat.message(ec.value());

std::cout << "Error description: " << description << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::error_category& cat = std::io_category();
std::string description = cat.message(ec.value());

std::cout << "Error description: " << description << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::error_category& cat = std::network_category();
std::string description = cat.message(ec.value());

std::cout << "Error description: " << description << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::system_error e(ec);

std::cout << "Error code: " << e.code() << '\n';
std::cout << "Message: " << e.what() << '\n';
```

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

```cpp
std::error_code ec = std::make_error_code(std::errc::invalid_argument);

std::system_error e = std::make_system_error(ec);

std::cout << "Error code: " << e.code() << '\n';
std::cout << "Message: " << e.what() << '\n';
```

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

```cpp
std::error_condition ec = std::make_error_condition(std::errc::invalid_argument);

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

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