Handling errors from POSIX APIs with C++
In POSIX-compliant systems, such as Unix and Linux, error handling is based on the use of error codes and error messages to communicate errors between functions and applications.
In general, when a function encounters an error, it returns a non-zero error code and sets the errno
global variable to a specific error value that indicates the nature of the error. The application can then use the errno
variable to determine the cause of the error and take appropriate action.
In addition to error codes, POSIX-compliant functions often provide error messages that describe the nature of the error in more detail. These error messages are typically accessed using the strerror
function, which takes an error code as input and returns a pointer to a sequence of characters terminated with a null character containing the corresponding error message.
The POSIX error-handling style requires developers to check for errors after each system call or function...