Renderingen-US

catch

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Starts a catch block.

Aliases: catch

Syntax: <structured-statement> catch <name> <block>

Category#

errors

Examples#

radix/corpus/cape/cape.fab (canonical · keyword)#

Starts a catch block.

# =============================================================================
# catch — Starts a catch block.
# =============================================================================
#
# What this teaches:
#   • Error recovery — `do { … } catch err { … }` attempts a block and catches
#     errors with a named handler
#   • Guarded execution — code inside the `do` block can `throw` errors that
#     are intercepted by the `catch` handler
#
# Common mistakes:
#   • attaching `catch` to a bare `{ }` block — `catch` only attaches to `do` blocks and structured statements; use `do { ... } catch err { ... }`
#
# See also: do, throw, ⇥
# =============================================================================

# catch — catch handler on do
#
# GRAMMAR:
#   facStmt :← 'do' block 'catch' ident block
#
# EXPECTED OUTPUT:
#   Scalar stdout smoke (see body).
#
# BACKEND:
#   Rust/Go: do/cape emit gap (whitelist: catch/cape.fab).
#

main {
    var int attempt ← 0
    do {
        attempt ← attempt + 1
        if attempt ≻ 1 {
            throw "simulated failure"
        }
        print "attempt §"(attempt)
    }
    catch err {
        print "caught"
    }
}

Expected output:

attempt 1

radix/corpus/cape/receptio-decline.fab (supporting · reject)#

catch AIR decline rows: whole-loop while/itera catch stays out under the loop-spectrum causes, a rethrow from a recovery arm declines (rethrow_in_recovery).

# =============================================================================
# receptio decline — the Tier 0.5 keep-outs stay fail-closed.
# =============================================================================
#
# Decline rows (reject reason in each comment):
#   1. whole-loop catch on `while` — one trailing catch wraps the whole loop
#      (never re-armed per iteration), and the loop spectrum stays an AIR
#      keep-out: SEM059 dum_loop. A loop statement has no normal-exit
#      value, so the recovery arm also fails the outer-type rule:
#      SEM059 recovery_type_mismatch
#   2. whole-loop catch on `for` — same per-loop attachment, same
#      keep-out: SEM059 itera_loop plus recovery_type_mismatch
#   3. rethrow — a throw from inside the catch arm is a rethrow; it
#      declines to Tier 2 fail-closed and is never silently treated as a
#      recovered value: SEM059 rethrow_in_recovery
#
# Common mistakes:
#   • expecting the trailing loop `catch` to recover per iteration — the
#      handler is per-loop by the grammar; per-iteration recovery would
#      need new syntax and its own goal
#   • expecting a rethrow to admit because the region admitted — the
#      recovery arm is checked as a catch region; throwing from it is the
#      named Tier 2 decline
#
# See also: catch, while, for, throw, receptio-air
# =============================================================================

# 1. whole-loop catch on while — loop-spectrum keep-out
@ radix lane "air"
fn custos_dum(bool pergo) → void {
    while pergo {
        throw "x"
    } catch err { }
}

# 2. whole-loop catch on for — loop-spectrum keep-out
@ radix lane "air"
fn custos_itera() → void {
    for range 0‥4 const i {
        throw "x"
    } catch err { }
}

# 3. rethrow — a throw inside the catch arm declines to Tier 2
@ radix lane "air"
fn reicit(bool malum) → int ⇥ string {
    do {
        if malum { throw "x" }
        return 1
    } catch err {
        throw "reicitur"
    }
    return 2
}

Expected: compilation rejects this example.