Book Deep-Read · A Philosophy of Software Design · Chapter 10
A Philosophy of Software Design · Ch 10 · Define Errors Out of Existence · Ousterhout · 2nd ed 2021
You tap "delete" on a photo that is already gone. Should your phone pop up "Error: photo does not exist," or just quietly say "Deleted"? Programs face this choice everywhere. Many people believe more errors means more rigor. This chapter says: every new kind of "error" is a chore that everyone using your code must deal with; the best error handling is making the error impossible in the first place.
You ask someone at home to "turn off the light." It is already off. A sensible person says "done," not "Report: the light is already off, unable to perform switch-off." What you wanted was the result, a dark room, not the act of flipping a switch. Phrase the request as a result, and the "error" vanishes.
Code that handles surprises almost never runs in daily life, so it is where unnoticed mistakes hide. Worse, once a part announces "I might fail this way," everyone who uses it must write their own response. Ten users means ten responses, and ten places to get it wrong.
Users of your code have fewer things to worry about, so there are fewer chances to slip. The test is simple: if the user found out about this error, could they do anything useful? If not, don't put it in front of them. One honest cost: don't overdo it. If the user truly needs to know (say, a message really didn't arrive), hiding it is lying to them.
Before you report an error, ask whether a better definition could make it not exist at all.
Want to see why exceptions are a major source of complexity, the four techniques for reducing exception handling, the Unix file-deletion and Python slicing examples, and where "hiding too much" begins? → Switch to Deep
You think throwing more exceptions is rigorous, a way of "surfacing problems." Ousterhout argues exceptions are one of the worst sources of complexity, and the best way to fight them is not to handle each one better but to reduce the number of places where exceptions must be handled. The first choice: redefine the interface's semantics so the error case simply doesn't exist.
try/catch and handles it. If nobody catches it, it keeps bubbling up.Chapter 10. Chapter 8 said "pull complexity downward" and Chapter 9 covered combining versus splitting; this chapter applies the same thinking to exception handling, the part of an interface's complexity people most often overlook. The next chapter turns to "design it twice." In practice it maps to every "should I throw here / return an error code?" decision, and to interfaces like rm -f, mkdir -p and DROP TABLE IF EXISTS.
The author first explains why exceptions create disproportionate complexity:
try/catch code is longer than the normal-case logic and buries it.Add a human factor: throwing exceptions is easy; handling them is hard. The author confesses his own mistake. When designing the Tcl language, he made unset (delete a variable) raise an error if the variable didn't exist. But the most common use of unset is cleaning up temporary state, and during cleanup you often don't know whether the variable was ever created. So almost everyone ended up wrapping unset in catch just to swallow the error. Every exception a class throws is part of its interface; the more exceptions, the shallower the module. Left alone, the complexity spreads up the call chain to every caller.
The author offers four techniques for reducing the places that handle exceptions, in order of preference: define them out, mask them, aggregate them, or just crash. Plus a generalization: design special cases out of existence too.
The core move is to restate an interface from "perform an action" to "guarantee a result." If Tcl's unset meant "make sure this variable doesn't exist," then a missing variable means the result is already achieved and there is nothing to report. The author gives two weightier examples.
Deleting an open file. On Windows, a file that some process has open cannot be deleted; the user must hunt down the process holding it and close it, and sometimes the only way out is a reboot. Unix instead removes the name from the directory immediately, lets processes that have it open keep reading and writing, and frees the data only when the last one closes it. The deleter never sees "file busy," and the user never sees "file vanished": errors on both sides are defined away at once.
Java's substring. substring(begin, end) throws IndexOutOfBoundsException whenever an index falls outside the string, so callers must check first and slice second. The author proposes instead: return the characters whose positions fall in [begin, end) and actually exist; anything out of range is simply empty. Python slicing already works this way: "Python"[4:42] yields 'on' with no error. Some object that this hides bugs. The author's answer, in essence: more error classes mean more code for callers and more places to go wrong; the best way to reduce bugs is to make software simpler.
If an exception can be handled at a low level, higher levels shouldn't hear about it. TCP is the classic case: the network drops packets, TCP retransmits internally, and applications see a reliable byte stream. Another is NFS: when a server crashes or stops responding, the client doesn't return an error to the application; it blocks and keeps retrying until the server comes back, printing "NFS server xxx not responding still trying" on the console. Users do complain when programs hang, but the author argues this beats the alternative: surface the error, and every application must handle "file server gone," where most could only exit and make the user start over. Masking is Chapter 8's pulling complexity downward: handle it once, and every layer above benefits.
Instead of handling each exception where it arises, let them bubble up to one place and handle a whole family with a single piece of code. The book's example is a web server: dozens of URL handlers each read request parameters, and students often write separate "missing parameter" logic in every handler. Better: have the parameter-fetching method throw one common "no such parameter" exception, and let the top-level dispatcher catch it and generate the error response. Handler code goes from dozens of copies to one, and new URLs need none.
Another form of aggregation is promoting a rare error into an existing, more common recovery path. The author cites the RAMCloud storage system he worked on: some uncommon faults get no dedicated repair logic and are instead funneled into the crash-recovery mechanism, which must exist anyway and runs often. Because that recovery code is exercised all the time, it is far more trustworthy than a special handler that almost never executes.
Some errors are both rare and nearly impossible to handle meaningfully; print diagnostics and abort. The classic is running out of memory: when C's malloc returns a null pointer, the caller can rarely do anything useful. Tcl wraps it as ckalloc, which panics on failure, so thousands of call sites never check a return value. Detecting that internal state has become inconsistent belongs here too. But it depends on the application: a replicated storage system cannot simply crash on a disk I/O error, because preserving data when hardware fails is its whole job.
The same idea applies to special cases in general. If an editor has a special "no selection" state, code everywhere must first check "is there a selection?" Make it always have a selection, possibly empty (start equals end), and those checks vanish: "delete selected text" on an empty selection does nothing, which is naturally correct.
Defining away or masking an exception is valid only if nobody outside the module needs the information. The author's counterexample is networking: a network module that swallowed every error would leave applications unable to learn that a message was lost, and so unable to recover. The test: when a caller gets this error, can and should it do something different? If so, keep it in the interface.
Table 1 · The four techniques: when to use them and what they cost
| Technique | Use when | Book example | Cost / risk |
|---|---|---|---|
| Define out | A broader semantics turns the "error" into a legal result | unset, Unix delete, substring returns the overlap | A caller with a real bug gets no warning; semantics must be documented in the interface |
| Mask | The lower layer can recover itself, and the upper layer could only give up anyway | TCP retransmit, NFS retry | May show up as hangs or slowness; over-masking blinds upper layers |
| Aggregate | Many sources are handled the same way, or can join an existing recovery path | Web dispatcher's single catch, RAMCloud | Errors must carry enough context, or the shared handler can only give vague messages |
| Just crash | Rare, not worth handling, and can't be handled well | Out of memory, inconsistent internal state | Wrong for systems whose job is fault tolerance (e.g. storage) |
| Throw as usual | The caller must act differently based on it | Telling apps a network message was lost | A more complex interface, but necessary complexity |
Table 2 · Two interfaces for the same job
| Action-style (breeds errors) | Result-style (error defined out) | Real-world counterpart |
|---|---|---|
| Delete this variable / file | Ensure it doesn't exist | rm -f, Go os.RemoveAll |
| Create this directory | Ensure it exists | mkdir -p |
| Drop this table | Drop it if it exists | SQL DROP TABLE IF EXISTS |
| Take characters begin through end | Take the characters that exist in that range | Python slicing |
One test: once this exception is thrown, do most callers just catch it and ignore it? If so, it should be defined out of existence.
This chapter is the most practical yardstick in an API review: for every new exception or error code, ask how many call sites will need another handler. It matters most in distributed systems, where error paths are most numerous and hardest to reproduce. Result-style, repeatable interfaces (retry as often as you like, same outcome) eliminate a large class of "what if the last attempt stopped halfway?" branches outright.
Evidence and sources · publicly verifiable
os.RemoveAll states that if the path does not exist, RemoveAll returns nil (no error). Source: golang/go · src/os/path.goIndexError: a range can be empty, but a single character can't be conjured from nothing. Source: python/cpython · Doc/tutorial/introduction.rst1. Exceptions are a major source of complexity: hard to handle, they breed new exceptions, the syntax is verbose, and they almost never run.
2. Research shows over 90% of catastrophic failures in distributed systems trace to incorrect error handling.
3. Throwing is easy, handling is hard; every exception a class throws is part of its interface, and more exceptions mean a shallower module.
4. The goal is not handling exceptions better but reducing the places that must handle them.
5. First choice, define them out: restate the interface from performing an action to guaranteeing a result (unset, Unix delete, substring returning the overlap).
6. Next, mask (TCP retransmit, NFS retry) and aggregate (one catch in the top-level dispatcher).
7. Rare, unrecoverable errors such as out-of-memory can just crash; but storage-like systems must handle I/O errors.
8. Special cases too: an empty selection instead of "no selection" removes every check.
9. The limit: if callers must act differently on an error, it stays in the interface.