Skip to content

Exceptions

Throw an exception with a tag.

Signature: (param args...)

Example:

(throw $error_tag (i32.const 500))

Throw an existing exception reference.

Signature: (param exnref)

Example:

(throw_ref (local.get $exn))

Rethrow an exception from a catch block.

Signature: (param i32)

Example:

(rethrow $label)

Declare an exception tag with a type signature for exception handling.

Example:

(tag $error (param i32))
(tag $complex_error (param i32 i32 f64))

Define a block that catches exceptions using a jump table.

Example:

(try_table (catch $tag $handler_label)
(throw $tag (i32.const 1))
)

Catches exceptions with a specific tag in a try_table block. Branches to a label with the exception payload.

Example:

(block $handler (result i32)
(try_table (catch $error_tag $handler)
(call $may_throw)
(i32.const 0) ;; No error
)
)
;; $handler receives the i32 payload from $error_tag
;; Multiple catch clauses
(try_table
(catch $error1 $handle_error1)
(catch $error2 $handle_error2)
(catch_all $handle_any)
(call $risky_operation))

Catches any exception in a try_table block, regardless of tag.

Example:

(block $fallback
(try_table (catch_all $fallback)
(call $may_throw_anything)
)
)
;; $fallback receives exnref

Catches exceptions with a specific tag and provides the exception reference.

Example:

(block $handler (result i32 exnref)
(try_table (catch_ref $error_tag $handler)
(call $may_throw)
(unreachable)
)
)
;; $handler receives payload and exnref for rethrowing

Catches any exception and provides the exception reference.

Example:

(block $handler (result exnref)
(try_table (catch_all_ref $handler)
(call $may_throw)
(unreachable)
)
)
;; Can rethrow with throw_ref