do
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Starts a scoped do-while style loop.
Aliases: do
Syntax: do <block> [catch <name> <block>] [while <condition>]
Category#
error
Related#
Examples#
radix/corpus/reference/keyword/fac/fac-cape.fab (canonical · keyword)#
Starts a scoped do-while style loop.
# =============================================================================
# do — Starts a scoped do-while style loop
# =============================================================================
#
# What this teaches:
# • Starts a scoped do-while style loop.
# • Related keywords: while, catch, lambda, continue
#
# Common mistakes:
# • Using `catch` on a non-`do` block — `catch` only attaches to `do` statements, not arbitrary blocks.
#
# See also: while, catch, lambda, continue
# =============================================================================
# do — try block with catch error handler
#
# do { <body> }
# catch err { <handler> }
#
# GRAMMAR:
# facStmt :← 'do' block 'catch' ident block
#
# EXPECTED OUTPUT:
# none — success path then simulated failure on attempt 2
#
# BACKEND:
# Rust/Go: do/cape not yet emitted — compile-only smoke.
main {
do {
print "Block executed successfully"
}
catch err {
print "Caught error: §"(err.nuntius)
}
# throw on second attempt; catch recovers and prints err.nuntius
mut int attempts ← 0
do {
attempts ← attempts + 1
print "Attempt §"(attempts)
if attempts ≡ 2 {
throw "Simulated failure"
}
}
catch err {
print "Failed on attempt §: §"(attempts, err.nuntius)
}
}Expected output:
Block executed successfully
Attempt 1
radix/corpus/reference/reject/fac/fac-cape-after-dum-reject.fab (supporting · reject)#
The handler precedes the loop condition; catch after while is refused by name.
# =============================================================================
# do — the error handler precedes the loop condition
# =============================================================================
#
# What this teaches:
# • The field order of a do statement is fixed: `do <block> catch <name>
# <block> while <condition>`. The handler binds before the loop condition, so
# a `catch` clause written after `while` has no place in the grammar and is
# refused by name (PARSE020 fac_cape_after_dum).
# • Write the handler first: `do { … } catch err { … } while <condition>`.
#
# Common mistakes:
# • spelling the loop as `do { … } while <cond> catch <err> { … }` — nothing
# after the condition accepts a handler; move `catch` before `while`.
# =============================================================================
main {
mut int i ← 0
do {
i ← i + 1
} while i ≺ 3 catch e {
print 1
}
}Expected: compilation rejects this example.