liveBook · Manning

Failure

As explained in The Art of Code

Failure is a program outcome in which an operation cannot complete its intended contract or produces an unsuccessful result.

Failure is an explicit unsuccessful outcome: an operation cannot fulfill its intended result, may complete only partly, or may return a recovery result such as cached data. Code should define success and failure early, make failure visible rather than silent, and release files, connections, sockets, and other resources on either path. section 2.5

Graceful failure handling is a design concern, not a patch added after the happy path. It protects robustness, leaves useful clues for diagnosis, preserves readable control flow, and avoids exposing users to mishandled errors. section 7.0 section 7.3

Choose the response by first identifying the kind of failure:

  • Business logic errors are expected violations of rules or usage constraints. Anticipate them, validate early, and guide the user back to a safe path. section 7.1.1 section 7.3
  • Technical problems arise from infrastructure or external dependencies, such as storage, databases, email, networking, or configuration. Do not leak raw technical details across UI or client boundaries; translate them into meaningful controlled responses while preserving the cause for diagnosis. section 7.1.2 section 7.3
  • Programming errors are code defects to correct during development. Fatal errors compromise process stability and generally call for code or infrastructure fixes, or a fail-fast response, rather than in-process recovery. section 7.3 section 7.1.4

A user takes an unexpected path

Invalid form input, an expired discount code, an unsupported upload, a duplicate email address, and too many login attempts are business-rule failures. Treating these as anticipated paths lets the application explain the situation and help the user proceed, rather than leaving an infinite spinner or an incomprehensible stack trace. section 7.1.1

Keep failure paths narrow and readable. Avoid generic catches without a clear reason; log only when it adds diagnostic value, with useful context and no sensitive data. A failure report should then be clarified into reproducible conditions before conclusions or fixes are attempted. section 7.3 section 10.3.2

Table 7.1 Summary of failure types and handling strategies
Type of failure
Description
Business logic errors
Violations of business rules or expected usage constraints that the system must anticipate and communicate clearly
Technical problems
Failures caused by external systems, infrastructure, configuration, or unavailable technical dependencies
Programming errors
Defects in the code itself caused by incorrect logic, invalid assumptions, or broken invariants
Fatal errors
Critical runtime failures that compromise process stability and usually prevent normal recovery

Compare the four failure categories, their sources, and the broad handling direction before deciding whether to validate, translate, fix, recover, or fail fast. section 7.1.4

How The Art of Code covers it

How deep it goes
Classifies four failure types and gives boundary, logging, translation, recovery, and fail-fast guidance without detailed implementations.
Examples
Uses invalid forms, expired discounts, unsupported uploads, duplicate emails, login limits, and infrastructure outages.
What you can do after
Apply it to classify failures and choose validation, controlled responses, diagnosis, recovery, or fail-fast handling.
How it connects
Connects failure to validation, exception translation, logging, recovery, fallback mechanisms, authentication, and authorization.

Also explained in

Compare how 12 books explain failure →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.