Errors, Resources, and Reliable Code
-
Raising an Error with a Clear Meaning
A raised error tells a caller that the promised operation could not complete normally. Its type and context help identify the next useful action.
-
Catching an Exception Near Its Source
An exception handler is easier to reason about when its protected block contains only the operation whose expected error it will translate.
-
Exception Propagation Across Function Calls
An exception that is unhandled in one call can travel to a caller able to handle it. Pending normal work along the way may be abandoned.
-
Cleanup with Finally
A finally block attempts cleanup on both ordinary and exceptional exits from a protected operation, subject to how the program and runtime exit.
-
Acquiring and Releasing Several Resources
When several resources have nested lifetimes, release them in an order consistent with their dependencies.
-
When Cleanup Fails After Another Error
A failed cleanup can add a second diagnostic after a primary operation has already failed. The result should keep enough context to explain both.
-
Temporary and Permanent Failures
A retry policy needs to know whether repeating the same operation with the same inputs might succeed without an unrelated change.
-
Fitting Retries Within a Total Deadline
Several attempts and their waits must fit the caller's deadline if the operation promises a bounded elapsed time.
-
Backoff and Jitter for Retried Work
Backoff can space out attempts after failures. Jitter can distribute those attempts so clients do not all resume at the same instant.
-
Timeouts and Unknown Outcomes
A timed-out caller can know it stopped waiting without knowing whether the requested operation completed on the other side.
-
Tracking Work Completed Before a Failure
When an operation consists of several effects, failure can leave some already completed. Record which steps actually happened before choosing recovery.
-
Collecting Several Validation Errors
Independent input errors can be reported together when each check has sufficient valid data to run.
-
Failing Early on Invalid Configuration
Configuration errors are easier to diagnose when checked before an application begins unrelated work using assumptions about the settings.
-
Fallbacks That Signal Reduced Functionality
A fallback can preserve part of a feature when the preferred data is unavailable. Its result should communicate any lost freshness or confidence.
-
Checking an Invariant at a Component Boundary
A boundary check can identify invalid state before another component assumes it is usable.
-
Closing a Resource More Than Once
An interface should state whether repeat cleanup succeeds safely, reports an already-closed state, or is outside its supported use.
-
Resource Lifetime When Ending Iteration Early
A consumer may stop before a source is exhausted. Cleanup must still occur according to who owns the underlying resource.
-
Cooperative Cancellation of a Task
A cancellation request can ask an ongoing operation to stop at a point where its state remains valid and cleanup is still possible.
-
Handling a Repeated Event
A processing system may receive the same event more than once. Applying it safely depends on its identity and the effect it requests.
-
Counting Outcomes in a Reliable Operation
An operation can track successes, expected rejections, and processing failures separately so monitoring explains how inputs moved through the system.
-
Turning a Known Error into a Structured Result
An interface can translate a well-understood error into fields used by callers while allowing unexpected failures to keep their diagnostic context.
-
Preserving User Input After an Ordinary Error
A recoverable validation or write error should leave relevant user input available when the user can correct and submit it again.
-
Error Codes and Human-Readable Messages
A stable code lets software branch on a result. A message explains the condition to a reader and can change with language or context.
-
Marking a Result as Stale
A useful result can be older than the consumer expects. Record the relevant observation time or freshness status explicitly so it can be used appropriately.
-
Recovering an Export Interrupted Before Publication
A restart can distinguish completed output from temporary work when publication is an explicit step with an identifiable result.
-
Recording a Checkpoint for Resumable Work
A checkpoint describes what work was committed so a restart can resume at a coherent boundary.
-
Resource Limits in an Interface Contract
A limit tells callers which inputs can be handled predictably. It needs a unit and a defined outcome at the boundary.
-
Invariants for a Work Queue
A queue operation should preserve counts, ordering, and which identities can still be processed even when a worker reports failure.
-
Diagnostic Context Without Copying Private Data
Operational diagnostics should include a location and failure reason while avoiding unnecessary copies of private or sensitive inputs.
-
Testing a Recovery Path with an Ordinary Simulated Failure
Inject a controlled failure into a dependency to check the state and result after an incomplete operation.