Stories: Error Handling
See also:
spec/error-handling.md— the rules this story explains.
Failure as a second exit, not a Result value
Error handling was one of the first features we designed, well before enums and variants existed in the language at all, because how a program fails is too important to bolt on late. So the answer a modern reader reaches for first — a Result<T, E> value the caller unwraps — was not even available when the question came up. That turned out not to matter, because even with variants in hand today we would not reach for one, and the reason has not changed since we set out.
We started not from a model but from a feel: what would a developer actually enjoy writing at the point a call can fail? Only once we had a shape we liked did we go looking at how other languages had solved it, as a check rather than a starting point. Two were real contenders. Go returns the error beside the value, content, err := ..., and the caller checks err != nil. Rust wraps the outcome in a Result and the caller matches on it or propagates it. The rest — C’s sentinel returns and out-of-band errno, unchecked exceptions that escape the type system — we set aside quickly, because a value you can forget to check, or a failure the compiler never made you acknowledge, is exactly the defect we wanted to make impossible.
What we disliked in the two contenders was subtler, and that dislike settled the shape. Go’s pair has two slots that are both always present: nothing in the type stops content and err from both being meaningful at once, so the contract “exactly one of these is real” lives only in a convention the compiler never checks. Rust’s Result fixes that — it is genuinely one-or-the-other — but it answers a question we did not think failure should have to raise. A variant is a general-purpose “one of several things,” the right tool for a value that legitimately comes in kinds; reaching for it to express failure treats an error as just another kind of value, when the whole point of an error is that it demands specific, unavoidable attention. The general construct was available; failure needed a specific construct.
So we asked the question that fixed the model: how does a function fail in the first place? Said plainly, the answer was that failing is a form of exiting — very close kin to return. Here is the picture that made it concrete. Rust receives a package, does its work, and sends one package back out through a single door: a green box on success, a red box carrying a message on failure, the color showing the caller which box it got. Go sends one package too — a box with two compartments inside. But a function has only ever needed one service from failure: an exit that is not the normal exit. A package with a color, or a box with two compartments, is an elaborate device for smuggling a second exit through one door. So we gave the function two doors. The primary door carries the ordinary return; the abort door carries the failure. A function that fails does not build a differently-colored package — it walks out the other door.
The surface fell out of that picture. If failure left by its own door, that door needed a declared type just as the primary door did, and it belonged right where the signature already declared the returned type: ReturnType?AbortType, the ? marking the fork between the two doors. At this stage, an abort door carrying no payload used Void, and abort was written bare; the later unit-type turn replaced that contract with Unit, an explicit abort Unit(), and a handler binder for the value. The same ? reappeared at the call site, where the caller had to state the outcome if the function took the abort door: call() ? err { ... }. That reuse was deliberate: the token carried one meaning in both places — the other door — so annotating a fork and handling a fork spent one symbol between them rather than two. Because an abort was just an exit and nothing more exotic, the constructs that compose expressions needed no special case for it: a match whose chosen arm could fail was itself a fallible expression, the abort door of the selected arm flowing straight through the match to the caller, neither introduced nor swallowed by it.
The cost here is the cost a reader expects us to name, and honesty runs the other way: it is very small. Because failure is a second exit rather than a value, you cannot pick up a success-or-failure outcome and carry it around as a first-class value — hold it in a list, stash it in a field, hand it back later. We went looking for where that bites and found little. A function’s failure is almost always dealt with where it happens; the one time you want to send it further along is to re-fail with it, and that is just taking the abort door yourself. The cases that look like a stored error usually are not errors at all — an unconfigured setting that falls back to a default, a lookup that legitimately finds nothing — and those read better as variants, which state out loud “this is not a failure, it is one of several normal outcomes.” So the one capability the two-doors model gives up, the language already covers better elsewhere, and the apparent cost mostly dissolves on inspection.
Handling a fork: resolve, and why it isn’t assignment
Once the caller must attach a handler to every call that can take the abort door, the handler’s job needs pinning down. When a function fails it has, by definition, not produced the value the caller was about to use, so a handler must first be able to supply a value in its place. The obvious first cut was to write that as an ordinary assignment — name the variable the call was feeding and set it from inside the handler:
content String = fs$read("main.py") ? msg {
content = String("# could not read")
}
It reads fine right up until the call is not feeding a variable. Put the same call inside a larger expression and there is no name to assign to:
print(fs$read("main.py") ? msg {
content = ??? // there is no `content` here to assign
})
A handler recovers a value for the call, and a call is an expression that can sit anywhere — as an argument, inside an operator, nested in another call — so tying recovery to a named destination only works in the single case where a variable happens to be present. What the handler actually needs is to substitute a value for the failed call expression itself, wherever that expression sits, and let the surrounding code carry on as though the call had returned it. That is resolve: assignment recovers for a variable, resolve recovers for an expression, and the expression case is the general case that contains the other.
Making recovery a keyword rather than a fall-off-the-end value bought a second property we wanted. A handler has three genuinely different moves — recover locally, or leave the whole function by one of its two doors — and each gets its own word: resolve exits only the handler and substitutes its value; return exits the parent function by the primary door; abort exits by the abort door. Because every exit is one of three named keywords, checking that a handler is exhaustive becomes trivial and, more to the point, independent of what anything is called — the compiler asks only whether every path ends in one of the three, never what the binder or some destination variable is named. Falling off the end of a handler is not a silent recovery-with-nothing; it is a compile error, because no keyword sits there. ?? is the single piece of sugar on top — expr ?? default is exactly a handler whose only move is resolve default — and it is sugar and nothing more, so one model still sits underneath.
Typed doors, and the propagate operator we don’t have
The third move a handler can make — abort err, re-failing with the error it was handed — is how a failure travels outward in Zane, and it is worth saying why that is a whole handler and not the single character Rust spends on the same job. Rust’s ? operator is a propagate: at any fallible call, ? means “if this failed, return its error from the enclosing function.” It is wonderfully terse, we do not have it, and the reason is not that we forgot to add the sugar.
The abort door is typed — a function declares exactly what its failure looks like, and that precision is one of the model’s central promises. A silent propagate operator quietly works against that promise. It type-checks cleanly only when the failure you are propagating is exactly the failure your enclosing function declares; the moment the two differ — the callee fails with a ParseError, you fail with an IoError — a bare propagate must either reject the code or convert the error for you. Rust chooses to convert: ? inserts a From call, silently building your error type out of the callee’s. That works because Rust has implicit From conversions to lean on. Zane converts no value implicitly, anywhere — an Int does not become a Float behind your back — and we were not going to carve a private exception into the error path, of all places, the one part of a program where a value quietly turning into another is least welcome. So a propagate operator has nothing to stand on here: without implicit conversion it would work only in the exact-match case, and in exactly that case abort err inside a handler already states the outcome clearly.
The cost is real and we name it: propagating a failure is more to type in Zane than in Rust — a binder and a block where Rust has one character. In return, every place a function can leave by its abort door is visible as a handler at the call site, no operator turns a fallible call into a hidden return, and no error ever changes type without someone having written the conversion. An explicit exit is worth a few characters; a reader who can see every way a function leaves is reading a more honest program than one scanning suffixes for the exits that slip out sideways.
Failing is not an effect
One question sits underneath all of this: is failing a side effect? Zane has an effect system that tracks whether a function mutates, reads, or stays pure, and abortability looks, at a glance, like one more property that system might track — one more admission a signature makes about a function’s behaviour. We keep the two entirely separate, and the history is part of the reason: error handling was designed first and the effect model came later, so when effects arrived there was nothing to reconcile, because the two had never described the same property.
They answer different questions. The effect system describes a function’s effect on the world — does it write memory, touch a capability, stay pure. Abortability describes the shape of a function’s return — can it leave by the abort door. Those axes are independent. A pure function that derives a result from its inputs and touches nothing else can still fail, the way parsing pure text can fail; a function that mutates the world freely can be infallible. All four combinations are ordinary, and none is stranger than the others. Folding “can fail” into the effect lattice would have forced a false coupling — it would seat failure on the same axis as mutation and imply some ordering or containment between them that simply is not there. Instead the compiler runs the two analyses independently and a call must satisfy both: the effect check and the abort check are separate gates the one call passes through. The cost is that a function’s contract now has two axes to read instead of one — but they are genuinely two axes, and collapsing them would have bought a tidier signature that described a less true design. The effect system names this orthogonality in its own words.
A Unit abort still carries a value
The original error rules treated Void as absence. A Void primary result could not be assigned, a Void abort had no binder, and bare resolve or abort carried nothing. Once the type became the real Unit value, that interpretation stopped holding: a successful call returns Unit(), and a failure with no information carries that same sole value down the abort path. Forbidding assignment would have recreated the pseudo-type distinction the type change removed.
We briefly kept the bare forms as convenience, then rejected them for the same reason bare returns went away. abort and resolve are AST exits with value expressions for every other type; omitting the expression for Unit would require each node and its type checker to recognise the nominal unit type and synthesize its constructor. A Unit abort therefore writes abort Unit(), a Unit recovery writes resolve Unit(), and a handler binds the abort value even when it contains no information.
The cost is visible ceremony on an empty door. The gain is that “no meaningful payload” no longer means “no typed value”: every path carries exactly the value its declared type promises, and error handling needs no special form for one bundled type.