Error Handling

Lesson 18 of 20

C has no exceptions — return codes, errno and perror are your only safety net, so learn to use all three.

The big picture

Errors travel through returns

C has no exceptions and no crash guards — errors travel back through return values, errno and stderr.

Return codes

Functions signal trouble with their return value — 0 usually means success.

errno remembers

Library calls store a small code in errno describing exactly what went wrong.

perror explains

perror("open") prints your word plus the system's own explanation of errno.

stderr for errors

Error messages belong on stderr — the diagnostics channel, separate from normal output.