Zane Error Handling
This document specifies Zane’s error handling model: bifurcated return paths, ? handler blocks, ?? shorthand, and compiler guarantees around abortability.
See also:
effects.md§7 for the relationship between effects and abortability.syntax.md§6 for the canonical surface syntax.
1. Overview
Zane uses a Bifurcated Return Path model in which success and failure are both explicit and statically typed.
Primary path. The normal return path uses the return type on the left of?.Abort path. The failure path uses the abort type on the right of?.Mandatory handling. Every abortable call must attach a?or??handler at the call site.
Story:
stories/error-handling.md— “Failure as a second exit, not aResultvalue”.
2. Core Concepts
2.1 Abort types are part of the signature
An abortable function declares:
ReturnType?AbortType
A declaration with no ?AbortType cannot abort. A reference-typed abort type carries ^ or & exactly as a reference-typed return type does (syntax.md §3.1), because an abort hands its value to the caller as a return does.
2.2 Unit abort type
If failure carries no meaningful payload, the abort type is Unit. The abort path still carries a value explicitly:
abort Unit();
2.3 Abortability is orthogonal to mut
Abortability and mutation are independent. A method may be:
- non-
mut, non-aborting - non-
mut, aborting mut, non-abortingmut, aborting
Story:
stories/error-handling.md— “Failing is not an effect”.
2.4 Abort type is structural, not behavioral
Changing a function’s abort type changes its function type. Abort types cannot be silently discarded when function values are passed around. Because callables are call-only, a function value is always a lambda-variable, and its declared function type must preserve the abort type of the lambda it holds.
parserOk Int?ParseError[String] = Int?ParseError(text String) { ... } // ok
parserBad Int[String] = Int?ParseError(text String) { ... } // ILLEGAL: abort type would be dropped
Story:
stories/error-handling.md— “Failure as a second exit, not aResultvalue”.
3. Call-Site Handling
3.1 ? handler blocks
Abortable calls are handled at the call site. One form is to attach a ? handler block:
value Int = parse("42") ? err {
resolve Int(0);
}
The binder is required even when the abort type is Unit:
done Bool = tryFinish() ? ignored {
resolve false;
}
There is no propagation-without-a-handler form. To pass failure outward, the handler itself uses abort ....
value Int = parse(input) ? err {
abort err;
}
Story:
stories/error-handling.md— “Typed doors, and the propagate operator we don’t have”.
3.2 Handler outcomes are exhaustive
Every path through a handler block must end in one of:
resolve ...return ...abort ...
Falling through a handler block is a compile-time error.
Story:
stories/error-handling.md— “Handling a fork:resolve, and why it isn’t assignment”.
3.3 ?? is resolve-only shorthand
expr ?? fallback desugars to a ? block that only resolves a default value.
count Int = parse("abc") ?? Int(0);
3.4 Unit primary returns are values
A call whose primary return type is Unit produces the Unit value returned by the callee. The result may be assigned or passed like any other value.
completed Unit = performWork();
When such a call is abortable, the handler attaches to the call expression exactly as for any other primary return type.
Story:
stories/error-handling.md— “AUnitabort still carries a value”.
3.5 match is abort-transparent
A match expression passes the output of its selected arm straight up. If the arms are abortable, the whole match is abortable and takes a ? (or ??) handler exactly like any other abortable expression. Abortability is not introduced or swallowed by match; it simply flows through.
result Int = match (token) {
x number => parse(x);
x symbol => lookup(x);
} ? msg {
console!print(msg);
resolve Int(20);
}
See also:
adt.md§5 for thematchexpression.
Story:
stories/error-handling.md— “Failure as a second exit, not aResultvalue”.
3.6 The program handles main’s abort
No call site calls main: the program calls it, and is its handler. An abort of main ends the program with status 1 after reporting the abort’s string (packages.md §6.3). This is why main’s one abort type is the string the program writes, @primitives$String (packages.md §6.2). An exported lambda, which a host calls, declares no abort type at all (packages.md §8.2): no handler waits on the host’s side, so the lambda handles every abort inside itself.
4. The resolve Keyword
resolve substitutes a value for the aborted call expression. It exits only the handler block.
| Keyword | Exits | Purpose |
|---|---|---|
resolve |
handler block | provide a value for the failed call expression |
return |
parent function | leave via the primary return path |
abort |
parent function | leave via the abort path |
When the primary return type is Unit, the handler writes resolve Unit(). resolve always carries an explicit value.
Story:
stories/error-handling.md— “Handling a fork:resolve, and why it isn’t assignment”.
5. Connection to the Effect Model
Abortability is not itself a side effect. A verb of any kind may be abortable or not, whatever it may write (effects.md §3). The effect system and the abort system are analyzed independently and combined by the compiler at the call site.
Story:
stories/error-handling.md— “Failing is not an effect”.
6. Compiler Guarantees
| Guarantee | Meaning |
|---|---|
| No unhandled aborts | Every abortable call must have a ? or ?? handler attached directly to that call expression; the program is the handler of main. |
| Exhaustive handlers | Every path through a handler block must terminate with resolve, return, or abort. |
| Return-path type safety | resolve values must match the primary return type; abort values must match the parent function’s abort type. |
| Abort-free functions stay abort-free | A function with no abort type is verified never to abort transitively. |
| No implicit abort-type dropping | Function-value assignments must preserve abort types exactly. |
7. Language Comparisons
7.1 Feature matrix
| Feature | Zane | C | Go | Java | Python | Rust | Swift | Zig |
|---|---|---|---|---|---|---|---|---|
| Unhandled failures are compile-time errors | ✅ | ❌ | ❌ | ⚠️ checked only | ❌ | ✅ | ✅ | ✅ |
| Failure type is part of the function type | ✅ | ❌ | ❌ | ⚠️ exception list only | ❌ | ✅ | ❌ | ✅ |
| Stack unwinding required | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ❌ |
| Inline recovery at call site | ✅ | ❌ | ❌ | ⚠️ verbose | ⚠️ verbose | ⚠️ verbose | ✅ | ✅ |
| Abort-free functions are statically guaranteed | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ |
7.2 Zane vs. C
C typically uses sentinel return values and out-of-band global error state.
C:
FILE *f = fopen("file.txt", "r");
if (f == NULL) {
return -1;
}
Zane:
file File = fs:open("file.txt") ? err {
abort err;
}
| Problem in C | How Zane solves it |
|---|---|
| Sentinel values can be ignored | Abortable calls must be handled. |
| Error state is often global | Failure type is carried in the signature. |
| Success and failure share one channel | Primary and abort paths are distinct. |
7.3 Zane vs. Go
Go exposes errors as a second return value that callers may ignore.
Go:
content, err := os.ReadFile("file.txt")
if err != nil {
return "", err
}
Zane:
content String = fs:readFile("file.txt") ? err { abort err }
| Problem in Go | How Zane solves it |
|---|---|
| Errors are optional to check | Handlers are mandatory. |
Boilerplate if err != nil blocks are repetitive |
? and ?? keep recovery adjacent to the call. |
| Error type is conventionally broad | Abort types are precise and structural. |
7.4 Zane vs. Java/Python exceptions
Java and Python use exceptions and stack unwinding.
Java:
try {
String content = readFile("file.txt");
} catch (IOException e) {
content = "default";
}
Zane:
content String = fs:readFile("file.txt") ? err {
resolve "default";
}
| Problem in exception models | How Zane solves it |
|---|---|
| Recovery is separated from the call site | Handler syntax is adjacent to the call. |
| Unwinding is runtime control flow | Zane uses explicit typed branches instead. |
| Unchecked exceptions escape type systems | Abort paths are always declared. |
7.5 Zane vs. Rust Result<T, E>
Rust represents fallibility as an explicit value that callers transform or propagate.
| Difference | Rust | Zane |
|---|---|---|
| Failure representation | Result<T, E> is a stored enum value |
Abortability is primarily a control-flow split at the call site |
| Inline recovery | combinators such as unwrap_or_else are available but can become expression-heavy |
? handlers keep multi-line recovery adjacent to the call |
| Propagation | ? propagates implicitly |
abort err inside the handler is explicit |
| Storing failures | Result<T, E> is already a value |
storing success/failure together requires an explicit wrapper such as a union |
7.6 Zane vs. Swift throws
Swift treats fallibility as a throwing effect with do/catch recovery.
| Difference | Swift | Zane |
|---|---|---|
| Error type in signature | throws does not name a concrete error type in the signature surface |
?AbortType names the failure type directly |
| Recovery form | do { try ... } catch { ... } wraps the call in a separate control structure |
the handler is attached directly to the call expression |
| Runtime model | uses exception-style unwinding | uses explicit typed branches at the call site |
| Coalescing | try? discards error information |
?? replaces an aborted call with a fallback value; abortability is still checked |
7.7 Zane vs. Zig error unions
Zig also keeps failure explicit and avoids stack unwinding, but the surface model differs.
| Difference | Zig | Zane |
|---|---|---|
| Signature order | Error!Value |
Value?Abort |
| Recovery syntax | catch with labeled-block patterns |
? with resolve/return/abort |
| Payload-free failure | inferred error sets and union mechanics | explicit Unit abort type |
| Integration with effects | no corresponding mut-based effect layer |
abortability and effects are analyzed separately; both must be satisfied at call sites |
8. Summary
| Concept | Rule |
|---|---|
| Abortable function | Declares ReturnType?AbortType |
? handler |
Mandatory for every abortable call |
| Handler paths | Must end with resolve, return, or abort |
?? |
Shorthand for resolve-with-default |
resolve |
Exits only the handler block |
Unit path value |
Unit() is a real primary or abort-path value and is written explicitly with return, resolve, or abort |
| Abort-free function | Statically guaranteed not to abort |