Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Error handling: bounded retry and fallback

Error handling separates detecting failure from deciding what to do next. Learn transient versus permanent failure, bounded retry, fallback, and idempotence through a deterministic std::expected example.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

std::expected<T, E>; std::unexpected{error_value}; result.has_value(); result.error(); *result; throw expression; try { /* ... */ } catch (const Exception& e) { /* ... */ }

Error handling means reporting failure and choosing an explicit response. In C++23, std::expected<T, E> holds either a value or an error; check has_value() before *result, and call error() only in the error state. Exceptions use throw and try/catch and can transfer control to a matching handler. Neither mechanism defines a recovery policy by itself. The supplied site publishes an evolving draft, not a frozen C++23 specification.

Transient versus permanent failure: a transient failure, such as temporary unavailability, may disappear without changing the request. In a retry policy, a failure is permanent if retrying the same operation under unchanged conditions is not expected to help. For example, an unsupported operation may require a different implementation or may have no remedy at all. This classification depends on the operation's contract, not automatically on its exception type. Bounded retry sets a maximum number of attempts, including the initial one: max_attempts = 3 means at most two retries. Stop on success, permanent failure, or exhaustion; for real I/O, also account for an overall deadline and delays between attempts.

Idempotence means repeating the same operation has the same intended effect as performing it once, not necessarily an identical response. After a timeout, the operation may already have changed state. Do not blindly retry payments or other side effects: require an idempotence guarantee or request deduplication. A fallback is an explicitly acceptable degraded result, not success of the original operation; propagate the error instead if no fallback is safe.

The example simulates a read without side effects. run(false) encounters two transient failures and obtains 42 on its third attempt. run(true) stops after the first permanent failure and keeps fallback equal to 7; exhausting the limit would also retain that value. Waiting is intentionally omitted for a deterministic demonstration. In production, preserve information about the cause and fallback use; this example is not a complete production policy.

Examples

#include <expected>
#include <iostream>

enum class Error { transient, permanent };

std::expected<int, Error> fetch(int attempt, bool denied) {
    if (denied) return std::unexpected(Error::permanent);
    if (attempt < 3) return std::unexpected(Error::transient);
    return 42;
}

void run(bool denied) {
    constexpr int max_attempts = 3;
    constexpr int fallback = 7;
    int value = fallback;
    int attempts = 0;

    for (int attempt = 1; attempt <= max_attempts; ++attempt) {
        ++attempts;
        auto result = fetch(attempt, denied);
        if (result.has_value()) {
            value = *result;
            break;
        }
        if (result.error() == Error::permanent) break;
    }

    std::cout << "value=" << value << " attempts=" << attempts << '\n';
}

int main() {
    run(false);
    run(true);
}

Result

value=42 attempts=3
value=7 attempts=1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources