Error handling idioms across languages
Compare exceptions (Python, Java checked and unchecked, JS/TS), Result/Option with ? (Rust) and error values (Go). For each, say where an error should be handled versus propagated and what 'ignoring' an error looks like.
Three 'small fixes' that compile
You asked the agent to add config loading to three services and get them building. Java now has `catch (IOException e) {}`, Go has `cfg, _ := Load(path)`, and Rust has `let _ = save(&cfg);`. Everything compiles and the tests are green. Each line looks like a harmless fix. Is it? Where should each of those errors actually go?
Handle, pass up, or drop
Every call that can fail forces a choice: handle the error here, pass it to the caller, or drop it. Java checked exceptions, Rust's `Result` and Go's `err` make that choice visible, and the compiler or a linter nags until you make it. Asked to make the build pass, the agent often takes the cheapest choice: drop. It compiles, and tests on good data stay green. **Later cost:** the program runs on with null or empty data and fails far away, with the cause lost.
Where errors go, language by language
**Rule:** handle an error only where you can act correctly, for example with a retry, a default the spec allows, a user message or an HTTP status. Everywhere else, propagate it with context.
- **Python, JS/TS:** propagate by not catching. Ignore: `except: pass`, empty `catch {}`, `.catch(() => {})`.
- **Java:** a checked exception must be caught or declared with `throws`. An unchecked one (`RuntimeException`) propagates without either. Ignore: empty `catch`.
- **Rust:** propagate an `Err` from a `Result`, or a `None` from an `Option`, with `?`. Ignore: `let _ =`, `.ok()`.
- **Go:** propagate with `return …, fmt.Errorf("x: %w", err)`. Ignore: `_` for err, or an err never checked.
Java: an empty catch after a compile error
The agent calls `Files.readString(path)`, and javac reports *unreported exception IOException*. The agent wraps the call in `try { … } catch (IOException e) {}` and returns `null`. **Tell:** an empty catch that appears right after a compile error. **Later cost:** a missing file turns into a `NullPointerException` in another class, with no trace of the path. **Better:** declare `throws IOException` if callers can react. Otherwise `throw new UncheckedIOException(e)`, which keeps the cause.
Go: the err that became _
Before the change, a failed `Load` returned early, with its error wrapped as `load: …`. Now the `_` drops that error. On failure, `Load` returns a nil `*Config`, so `cfg.DSN` panics with a nil pointer dereference, and the crash says nothing about the missing file. **Tell:** an `err` replaced by `_`. **Better:** keep the removed lines. Because `%w` wraps the error, callers can still check `errors.Is(err, fs.ErrNotExist)`.
// Go
--- a/server.go
+++ b/server.go
@@ -12,8 +12,5 @@
- cfg, err := Load(path)
- if err != nil {
- return nil, fmt.Errorf("load: %w", err)
- }
+ cfg, _ := Load(path)
db, err := Open(cfg.DSN)
if err != nil {
return nil, err
}
Every failure needs a destination
Handle an error where you can act correctly; everywhere else, propagate it with context (`?`, `throws`, `return …, err`, or simply not catching). An empty `catch`, `except: pass`, `let _ =`, `.ok()` or `_` for an err is ignoring, so ask: where does this failure end up?
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.