Error Handling¶
The errno
Variable¶
On error, system calls (and most C library functions) return -1 and
set the global variable errno
.
ssize_t n = read(fd, buffer, sizeof(buffer));
if (n == -1)
if (errno == EINTR) {
/* interrupted system call, retry possible */
...
}
else {
/* abort, reporting the error */
...
}
errno
Is A Global Variable¶
Where’s the bug?
ssize_t n = read(fd, buffer, sizeof(buffer));
if (n == -1) {
fprintf(stderr, "Error %d\n", errno);
if (errno == EINTR)
/* ... */
}
Helper Functions¶
Prototype |
Description |
Documentation |
---|---|---|
|
Message to |
|
|
Modifiable pointer to error description |
|
|
Cleanest alternative: |
if (n == -1)
perror("read()");