# errno

***

**1. Checking for Errors after File Opening**

```cpp
#include <errno.h>
#include <iostream>

int main() {
  FILE* file = fopen("myfile.txt", "r");
  if (file == NULL) {
    std::cerr << "Error opening file: " << strerror(errno) << "\n";
    return EXIT_FAILURE;
  }
  // File opened successfully, perform operations...
  fclose(file);
  return EXIT_SUCCESS;
}
```

**2. Handling System Call Errors**

```cpp
#include <errno.h>
#include <iostream>

int main() {
  int status = system("ls -l");
  if (status == -1) {
    std::cerr << "Error executing system call: " << strerror(errno) << "\n";
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
```

**3. Displaying Detailed Error Information**

```cpp
#include <errno.h>
#include <iostream>
#include <stdexcept>

int main() {
  try {
    // Perform an operation that may fail...
  } catch (std::exception& e) {
    std::cerr << "Error: " << e.what() << "\n";
    std::cerr << "errno: " << errno << "\n";
    std::cerr << "strerror: " << strerror(errno) << "\n";
  }
  return EXIT_SUCCESS;
}
```

**4. Retrieving the Error Number after a Failed Operation**

```cpp
#include <errno.h>
#include <iostream>

int main() {
  int result = some_function();
  if (result == -1) {
    std::cerr << "Error code: " << errno << "\n";
    std::cerr << "Error message: " << strerror(errno) << "\n";
  }
  return EXIT_SUCCESS;
}
```

**5. Testing for Specific Error Conditions**

```cpp
#include <errno.h>
#include <iostream>

int main() {
  int status = system("ls -l");
  if (status == -1 && errno == ENOENT) {
    std::cerr << "The specified file or directory does not exist.\n";
  }
  return EXIT_SUCCESS;
}
```

**6. Using errno in a Custom Error Class**

```cpp
#include <errno.h>
#include <stdexcept>

class MyError : public std::runtime_error {
public:
  MyError(const std::string& message)
    : std::runtime_error(message + ": " + strerror(errno)) {}
};
```

**7. Setting errno in a Custom Function**

```cpp
#include <errno.h>
#include <iostream>

void my_function() {
  // Perform an operation that may fail...
  if (some_condition) {
    errno = EBADF;
  }
}
```

**8. Checking for Errors after a Socket Operation**

```cpp
#include <errno.h>
#include <iostream>
#include <sys/socket.h>

int main() {
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    std::cerr << "Error creating socket: " << strerror(errno) << "\n";
    return EXIT_FAILURE;
  }
  // Socket created successfully, perform operations...
  close(sock);
  return EXIT_SUCCESS;
}
```

**9. Retrieving the Last Error Occurred in a Thread**

```cpp
#include <errno.h>
#include <iostream>
#include <pthread.h>

void* thread_function(void*) {
  // Perform an operation that may fail...
  if (some_condition) {
    errno = EBADF;
  }
  return NULL;
}

int main() {
  pthread_t thread;
  pthread_create(&thread, NULL, thread_function, NULL);
  pthread_join(thread, NULL);
  std::cerr << "Error code: " << errno << "\n";
  std::cerr << "Error message: " << strerror(errno) << "\n";
  return EXIT_SUCCESS;
}
```

**10. Using errno in a Function Pointer**

```cpp
#include <errno.h>
#include <iostream>

typedef int (*function_pointer_t)(int);

int my_function(int x) {
  // Perform an operation that may fail...
  if (some_condition) {
    errno = EBADF;
  }
  return x;
}

int main() {
  function_pointer_t func = &my_function;
  int result = func(42);
  if (result == -1) {
    std::cerr << "Error code: " << errno << "\n";
    std::cerr << "Error message: " << strerror(errno) << "\n";
  }
  return EXIT_SUCCESS;
}
```

**11. Using errno in a Signal Handler**

```cpp
#include <errno.h>
#include <iostream>
#include <signal.h>

void signal_handler(int signal) {
  // Perform an operation that may fail...
  if (some_condition) {
    errno = EBADF;
  }
}

int main() {
  signal(SIGINT, signal_handler);
  // Wait for a signal to occur...
  return EXIT_SUCCESS;
}
```

**12. Checking for Errors after a Memory Allocation**

```cpp
#include <errno.h>
#include <iostream>
#include <malloc.h>

int main() {
  void* ptr = malloc(1024);
  if (ptr == NULL) {
    std::cerr << "Error allocating memory: " << strerror(errno) << "\n";
    return EXIT_FAILURE;
  }
  // Memory allocated successfully, perform operations...
  free(ptr);
  return EXIT_SUCCESS;
}
```

**13. Handling Errors in a Database Connection**

```cpp
#include <errno.h>
#include <iostream>
#include <mysql/mysql.h>

int main() {
  MYSQL* conn = mysql_init(NULL);
  if (conn == NULL) {
    std::cerr << "Error initializing MySQL connection: " << mysql_error(conn) << "\n";
    return EXIT_FAILURE;
  }
  // Connection initialized successfully, perform operations...
  mysql_close(conn);
  return EXIT_SUCCESS;
}
```

**14. Displaying Error Messages in a Web Server**

```cpp
#include <errno.h>
#include <iostream>
#include <sstream>

class HTTPServer {
public:
  std::string handle_request(const std::string& request) {
    // Parse the request...
    if (some_condition) {
      std::stringstream ss;
      ss << "Error: " << strerror(errno) << "\n";
      return ss.str();
    }
    // Handle the request successfully, return a response...
  }
};
```

**15. Checking for Errors after Opening a File Descriptor**

```cpp
#include <errno.h>
#include <iostream>
#include <fcntl.h>

int main() {
  int fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    std::cerr << "Error opening file: " << strerror(errno) << "\n";
    return EXIT_FAILURE;
  }
  // File opened successfully, perform operations...
  close(fd);
  return EXIT_SUCCESS;
}
```

**16. Handling Errors in a Network Operation**

```cpp
#include <errno.h>

```
