liveBook · Manning

Exception

As explained in Modern C, Third Edition

An exception is an abnormal execution condition that can interrupt ordinary computation and potentially terminate a program.

An output failure

A call to puts can fail instead of printing. Check its return value: `if (puts("hello world") == EOF) { perror("can't output to terminal:"); exit(EXIT_FAILURE); }`. The diagnostic makes the failure visible; `exit` ends execution rather than letting the program continue as though output succeeded. section 8.1.3

An exceptional condition can also arise during arithmetic. Integer division or modulo by zero has no mathematical result. Negating `INT_MIN` and certain bit shifts likewise exceed the operations permitted for their operands. Such operations may crash on some platforms; the safe rule is to perform arithmetic only when the result is defined within the underlying type's range. section 15.1.1

Floating-point arithmetic needs a separate check: where infinity is supported, division by zero can produce infinity. `<fenv.h>` names conditions such as `FE_DIVBYZERO`, `FE_INVALID`, and `FE_OVERFLOW`; `feclearexcept` clears indicators before an operation, and `fetestexcept` inspects them afterward. Which macros are available is platform-dependent. section 15.1.1

An exception or other failure does not prescribe one response in C:

  • Check library return values and report failures rather than ignoring them. A function may also communicate a specific error through `errno`. section 8.1.3
  • When returning an error from a function, preserve required state and release acquired resources. For example, an output failure after `malloc` must not skip `free`. section 15.6
  • For a condition that cannot be repaired locally, `longjmp` can transfer control to a target established by `setjmp`; it does not return to the call that invoked it. section 19.5
Figure 19.2 Control flow with setjmp and longjmp: longjmp jumps to the position marked by setjmp.
figure

The control-flow diagram shows the distinction between recognizing a condition where it occurs and resuming at a previously established jump target. section 19.5

How Modern C, Third Edition covers it

How deep it goes
Works through arithmetic cases, floating-point indicators, error cleanup, and setjmp/longjmp rather than treating every failure as one mechanism.
Examples
Uses division by zero, checked puts output, a number-printing function with cleanup, and a recursive parser with a jump target.
What it assumes
Assumes familiarity with C functions and control flow; builds the cleanup discussion after pointers and introduces floating-point condition macros where needed.
What you can do after
Diagnose/debug with return-value checks and floating-point indicators; practice/perform cleanup or establish a setjmp target for unrepairable conditions.
How it connects
Connects exceptions to undefined arithmetic, floating-point environment flags, library return values, errno, resource cleanup, and setjmp/longjmp.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.