Returning an Error Result or Raising an Exception
A routine can signal failure through a structured result or by raising an exception. The caller needs a clear rule for recognizing and handling that outcome.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Define normal absence first
Looking up a catalog item by its ID may regularly find nothing. A distinct missing result can represent that outcome, while a storage failure raises or returns a separate error. The distinction between absent and empty values helps avoid an ambiguous channel.
Exercise both paths
Use one absent ID and one unavailable test data source. If both return a bare empty object, callers lose the ability to report the storage failure. Return values and side effects describe the observations the routine may expose.[1][2]
Give the caller enough information to react
A search over a file can conclude that a requested identifier is missing after reading every record. An unreadable file instead leaves the search incomplete. A caller can offer a create action for a missing record and report access failure for an unreadable file. Returning None for both prevents that distinction. A result record can encode ordinary domain outcomes, while an exception may carry a system failure through unrelated layers. See returning a decision with a reason.
Handle failures at a boundary that has context
A low-level decoder can identify an invalid field offset without knowing whether the overall import should stop or skip that record. An importer can make that policy decision and attach the original line number. Test two malformed records separated by a valid one under both fail-fast and skip-invalid contracts when your importer supports both. Record which side effects occur before the error. See validating parsed values for another place where errors acquire domain meaning.