Commit 3bdd53d3

3bdd53d3a6c3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · +1 added · ~1 modified

Description

Add catch/throw

catch(:tag) { block } → setjmp, run block, match tag on longjmp throw :tag, value → set tag+value, longjmp with code 2

Features:

  • Nested catch: inner re-throws to outer if tag doesn't match
  • catch without throw returns block's last expression
  • throw in both statement and expression context
  • Integer and string throw values

Test: examples/bm_catch.rb — basic, nested, and no-throw cases.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
3bdd53d3a6c3
Parent
a154ae57b25c

examples/bm_catch.rbaddedView file

1+# Test catch/throw
2+
3+result = catch(:done) do
4+ throw :done, 42
5+ 999 # unreachable
6+end
7+puts result # 42
8+
9+# catch without throw returns block value
10+result2 = catch(:nope) do
11+ 100
12+end
13+puts result2 # 100
14+
15+# Nested catch (same type)
16+result3 = catch(:outer) do
17+ catch(:inner) do
18+ throw :outer, 77
19+ end
20+ 0
21+end
22+puts result3 # 77
23+
24+puts "done"