An exception is an abnormal execution condition that can interrupt ordinary computation or terminate a program. In C, different conditions call for different responses: avoid invalid arithmetic, check library error returns, or transfer control when a condition cannot be repaired locally. section 15.1.1 section 8.1.3 section 19.5
Checking an output failure
if (puts("hello world") == EOF) { perror("can't output to terminal:"); exit(EXIT_FAILURE); } The return value signals failure. perror prints a diagnostic using the error state, and exit ends execution. Stopping immediately can make a failure easier to detect during development. section 8.1.3
Library functions use several error-return conventions, including null pointers, special codes, and negative values. Some also report a specific error through errno. If an error is recoverable, manage the error state as well as the return value; cleanup may also need to release allocated memory. section 8.1.3 section 15.6
Table 8.2 Error return strategies for C library functions. Some functions may also indicate a specific error condition through the value of the errno macro.
| Failure return | Test | Typical case | Example |
| Null pointer | !value |
Other values are valid | fopen |
| Special error code | value == code |
Other values are valid | puts, clock, mktime, strtod, fclose |
| Nonzero value | value |
Value otherwise unneeded | fgetpos, fsetpos |
| Special success code | value != code |
Case distinction for failure condition | thrd_create |
| Negative value | value < 0 |
Positive value is a counter | printf |
Compare error-return strategies before choosing a test for a library call. section 8.1.3
An arithmetic exceptional condition includes an operation for which no mathematical result is defined, such as integer division or modulo by zero. Finite representation also creates hazards, including negating INT_MIN and invalid shift counts. Perform arithmetic only when the operation is defined within the range of its underlying type. section 15.1.1
Floating-point arithmetic needs separate treatment: where infinity is supported, division by zero can produce infinity. The floating-point environment provides macros for conditions including division by zero, inexact results, invalid operations, overflow, and underflow. These conditions can be combined as bitsets, tested with fetestexcept, and cleared with feclearexcept. section 15.1.1
For an unrecoverable condition deep in a call sequence, setjmp establishes a target and longjmp transfers control to it. The jmp_buf target must still be alive and valid. Unlike a goto label, it is not limited to a target in the same function; longjmp never returns to its caller. section 19.5
Listing 19.2 The user interface for the recursive descent parser
void basic_blocks(void) {
char buffer[maxline];
unsigned depth = 0;
char const* format =
"All matching %0.0d'%c' '%c' pairs have been closed correctly\n";
jmp_buf jmpTarget;
switch (setjmp(jmpTarget)) {
case 0:
descend(nullptr, &depth, maxline, buffer, jmpTarget);
break;
case plusL:
format =
"Warning: %d '%c' have not been closed properly (expected '%c')\n";
break;
case plusR:
format =
"Error: closing too many (%d) '%c' parenthesis with
additional '%c'\n";
break;
case tooDeep:
format =
"Error: nesting (%d) of '%c' '%c' constructs is too deep\n";
break;
case eofOut:
format =
"Error: EOF for stdout at %d open '%c', expecting same amount
of '%c'\n";
break;
case interrupted:
format =
"Interrupted at level %d of '%c' '%c' nesting\n";
break;
default:;
format =
"Error: unknown error within (%d) '%c' '%c' constructs\n";
}
fflush(stdout);
fprintf(stderr, format, depth, left, right);
if (interrupt) {
SH_PRINT(stderr, interrupt,
"is somebody trying to kill us?");
raise(interrupt);
}
}
A recursive parser uses the setjmp result to distinguish normal completion from unmatched delimiters, excessive nesting, output failure, and interruption. section 19.5
























