Exceptions
Traps are fatal. Languages such as C++, C#, Kotlin and OCaml also need recoverable exceptions that unwind the stack to the nearest handler. Without native support, compilers had to emulate them with costly tricks (checking a flag after every call, or round-tripping through JavaScript). The exception handling feature, supported in all major browsers since 2022 and part of WebAssembly 3.0, adds them directly.
Tags: the type of an exception
An exception is thrown with a tag, which declares what values it carries. Think of a tag as an exception class with typed fields:
(tag $not_found (param i32)) ;; carries one i32 (e.g. an id)
(tag $parse_error (param i32 i32)) ;; carries two values (line, column)throw $tag pops the tag's values and starts unwinding. A handler for that tag receives the values.
try_table and catch
The current form of the feature uses try_table: a block with a list of catch clauses. Each clause names a tag
and a label to branch to when that tag is caught. The caught values arrive on the stack at that label. Here
$safe returns x * 10, or 1000 + |x| when $check throws:
(module
(import "console" "log" (func $log (param i32)))
(tag $negative (param i32))
(func $check (param $x i32) (result i32)
(if (i32.lt_s (local.get $x) (i32.const 0))
(then (throw $negative (local.get $x))))
(local.get $x))
(func $safe (param $x i32) (result i32)
(local $bad i32)
(block $caught (result i32)
(return
(try_table (result i32) (catch $negative $caught)
(i32.mul (call $check (local.get $x)) (i32.const 10)))))
(local.set $bad) ;; the thrown value
(i32.add (i32.const 1000) (i32.sub (i32.const 0) (local.get $bad))))
(func (export "main")
(call $log (call $safe (i32.const 4)))
(call $log (call $safe (i32.const -7)))))40
1007
The exception was thrown inside $check, one call deeper than the handler. Unwinding popped $check's frame and
continued at $caught in $safe.
Catch clauses
| Clause | Catches | Pushes at the label |
|---|---|---|
catch $tag $l |
exceptions with $tag |
the tag's values |
catch_ref $tag $l |
exceptions with $tag |
the values and an exnref |
catch_all $l |
any exception (including JS ones) | nothing |
catch_all_ref $l |
any exception | an exnref |
An exnref is a reference to the caught exception that you can re-throw later with throw_ref, for example after
running cleanup code (like finally):
(module
(import "console" "log" (func $log (param i32)))
(tag $fail (param i32))
(func $work (param $n i32)
(call $log (local.get $n))
(if (i32.eq (local.get $n) (i32.const 2))
(then (throw $fail (i32.const 42)))))
;; runs $work, logs -1 as "cleanup" no matter what, and re-throws on failure
(func $with_cleanup (param $n i32)
(local $exn exnref)
(block $on_error (result exnref)
(try_table (catch_all_ref $on_error)
(call $work (local.get $n)))
(call $log (i32.const -1)) ;; cleanup on success
(return))
(local.set $exn)
(call $log (i32.const -1)) ;; cleanup on failure
(throw_ref (local.get $exn))) ;; propagate the same exception
(func (export "main") (result i32)
(block $outer (result i32)
(try_table (catch $fail $outer)
(call $with_cleanup (i32.const 1))
(call $with_cleanup (i32.const 2))
(call $with_cleanup (i32.const 3)))
(i32.const 0))))1
-1
2
-1
42
Step by step: 1 succeeds (prints 1, then cleanup -1); 2 throws (prints 2, cleanup -1, re-throw); the
outer handler in main catches the payload 42, which becomes main's result. 3 never runs.
The legacy try … catch syntax
Browsers first shipped an earlier version of the proposal with try/catch/delegate/rethrow. You'll still
see it in older toolchain output, and engines still accept it:
(try (result i32)
(do (call $might_throw))
(catch $negative ;; payload on the stack
(i32.const -1)
(drop))
(catch_all (i32.const 0)))New code (and current LLVM/Binaryen) uses try_table with exnref.
Exceptions and JavaScript
JavaScript can create tags and catch Wasm exceptions too:
new WebAssembly.Tag({ parameters: ['i32'] })creates a tag that a module can import.- A Wasm exception reaching JavaScript is a
WebAssembly.Exception;e.is(tag)checks its tag ande.getArg(tag, 0)reads its payload. - A JavaScript exception thrown inside an imported function unwinds through Wasm and can be caught there with
catch_all.
The module below imports a tag env.err and exports check(x), which throws err(x) for negative x:
const bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x0a, 0x02, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x01, 0x7f, 0x01, 0x7f, // types: (i32)->(), (i32)->i32
0x02, 0x0c, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x03, 0x65, 0x72, 0x72, // import "env" "err"...
0x04, 0x00, 0x00, // ...kind 4 = tag, type 0
0x03, 0x02, 0x01, 0x01, // one function of type 1
0x07, 0x09, 0x01, 0x05, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x00, 0x00, // export "check"
0x0a, 0x12, 0x01, 0x10, 0x00,
0x20, 0x00, 0x41, 0x00, 0x48, // local.get 0, i32.const 0, i32.lt_s
0x04, 0x40, // if (no result)
0x20, 0x00, 0x08, 0x00, // local.get 0, throw tag 0
0x0b, 0x20, 0x00, 0x0b, // end, local.get 0, end
]);
const err = new WebAssembly.Tag({ parameters: ['i32'] });
const { instance } = await WebAssembly.instantiate(bytes, { env: { err } });
for (const x of [5, -3]) {
try {
console.log('ok', instance.exports.check(x));
} catch (e) {
console.log(e instanceof WebAssembly.Exception, e.is(err), e.getArg(err, 0));
}
}ok 5
true true -3
Exceptions vs traps
| Trap | Exception | |
|---|---|---|
| Caused by | bugs: bad memory access, division by zero, unreachable |
throw, or a JS exception |
| Catchable in Wasm | no | yes (try_table) |
| Carries data | no | yes, the tag's values |
| Typical use | aborting | C++ throw, Kotlin/C# exceptions, OCaml effects |
Test yourself
What does a tag define?
- A label for
br - The types of the values an exception carries
- A memory region
- An export name
Which catch clause gives you an exnref for any exception?
catchcatch_refcatch_allcatch_all_ref
Can a trap be caught with try_table?
- Yes, with
catch_all - No, traps are not exceptions
- Only
unreachable - Only in Node.js