---
title: "Errors and nulls · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/errors-and-nulls
updated: 2026-10-20
licence: "All rights reserved"
---

# Errors and nulls

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

> Throw clear, typed errors instead of returning codes. Never return or pass null, and give every field a value unless absence is a valid state.

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/errors-and-nulls`.

## Required

- [Throw exceptions instead of returning error codes](https://agentwranglers.se/playbook/architecture/errors-and-nulls/exceptions-not-error-codes): Signal failure with an exception or a typed result. Never return a magic value such as `-1`, `false` or an error string that callers must remember to check.
- [Never return null](https://agentwranglers.se/playbook/architecture/errors-and-nulls/never-return-null): Collections return empty, services use a Null Object, and lookups return one consistent "absent" value. A caller should never crash because it forgot a check.
- [Never pass null into a function](https://agentwranglers.se/playbook/architecture/errors-and-nulls/never-pass-null): Never call a function with `null` to mean "use the default" or "skip this". Give the function a clear variant or an options object instead.
- [Make fields required unless absence is valid](https://agentwranglers.se/playbook/architecture/errors-and-nulls/required-fields): Every constructor argument and data object field is required and non-nullable, unless "not there" is a valid state of the business.
- [Never swallow errors](https://agentwranglers.se/playbook/architecture/errors-and-nulls/never-swallow-errors): Never catch an error and carry on as if nothing happened. Handle it, add context and rethrow it, or let it reach the edge.
- [Release every resource you open](https://agentwranglers.se/playbook/architecture/errors-and-nulls/release-resources): Close every file, connection, lock and handle with the language's scoped construct, so it is released on success and on error.
- [Await every async call](https://agentwranglers.se/playbook/architecture/errors-and-nulls/await-every-promise): Await every promise, task or future, or hand it to code that handles its errors. A call that nobody waits for loses its errors without a trace.

## Recommended

- [Use typed domain exceptions](https://agentwranglers.se/playbook/architecture/errors-and-nulls/domain-exceptions): Give each kind of failure its own exception type with a clear name and the data needed to handle it. Never throw a bare generic error with only a message.

- Previous: [Comments](https://agentwranglers.se/playbook/architecture/comments)
- Next: [Coupling](https://agentwranglers.se/playbook/architecture/coupling)
- [All chapters](https://agentwranglers.se/playbook/architecture)
