Skip to content
Latchkey

What Is Error Handling? Dealing With Failure Gracefully

Error handling is how a program anticipates, detects, and responds to failures so it can recover or fail gracefully instead of crashing unpredictably.

Things go wrong: files are missing, networks drop, inputs are malformed. Error handling is the discipline of expecting those failures and deciding what to do about them, retry, fall back, report, or exit cleanly, rather than letting them crash the program or, worse, corrupt data silently. Good error handling is what separates fragile code from robust software.

Ways to handle errors

  • Exceptions with try/catch to separate error logic from the happy path.
  • Error return values that callers must explicitly check.
  • Result or Option types that force handling at compile time.
  • Retries with backoff for transient, recoverable failures.

Fail loud, not silent

The worst error handling is swallowing an error and continuing as if nothing happened, which hides bugs and corrupts state. Good handling either recovers meaningfully or surfaces the failure clearly, never quietly ignores it.

Transient vs permanent errors

Some failures are transient (a momentary network blip) and worth retrying; others are permanent (invalid input) and retrying just wastes time. Distinguishing the two is central: retry the recoverable, fail fast on the rest.

Graceful degradation

Robust systems degrade gracefully: if a non-critical service fails, the app continues with reduced functionality rather than collapsing entirely. Error handling decides which failures are fatal and which can be tolerated.

A quick example

Wrapping a network call in retry-with-backoff handles a transient blip automatically, while a clear error is raised if the call keeps failing after several attempts.

Error handling in CI

CI fails for two reasons: real bugs (deterministic, fix them) and transient infrastructure hiccups (a flaky network or registry, retry them). Latchkey applies exactly this logic, auto-retrying transient failures like network blips or OOM kills while letting genuine errors fail so they get fixed.

Key takeaways

  • Error handling anticipates and responds to failures so programs degrade gracefully.
  • Never silently swallow errors: recover meaningfully or surface them clearly.
  • Distinguishing transient from permanent failures decides what to retry versus fix.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →