Exceptions and traps
editusing var runtime = new WasmRuntime();
var module = runtime.Compile(WasmRuntime.WatToWasm("""
(module
(import "env" "fail" (func $fail))
(tag $error (export "error") (param i32))
(func (export "throw") (param i32) (throw $error (local.get 0)))
(func (export "catch") (result i32)
(block $handler (result i32)
(try_table (catch $error $handler) (call $fail))
(i32.const -1)))
(func (export "divide") (param i32 i32) (result i32) (i32.div_s (local.get 0) (local.get 1))))
"""));
using var store = runtime.CreateStore();
WasmInstance? instance = null;
// A host function throws an exception in WebAssembly with a WasmThrowException
var fail = store.CreateFunction(new WasmFunctionType([], []), (arguments, results) =>
throw new WasmThrowException(store.CreateException(instance!.GetTag("error")!, [WasmValue.FromI32(42)])));
instance = store.Instantiate(module, [fail]);
Assert.Equal(42, instance.GetFunction("catch")!.Call()[0].I32);
// An exception thrown by WebAssembly is a WasmThrowException
var thrown = Assert.Throws<WasmThrowException>(() => instance.GetFunction("throw")!.Call(WasmValue.FromI32(7)));
Assert.Same(instance.GetTag("error"), thrown.Exception.Tag);
Assert.Equal(7, thrown.Exception.GetArgument(0).I32);
// A trap is a WasmTrapException
var trap = Assert.Throws<WasmTrapException>(() => instance.GetFunction("divide")!.Call(WasmValue.FromI32(1), WasmValue.FromI32(0)));
Assert.Equal(WasmTrapKind.Other, trap.TrapKind);| Exception | Thrown when |
|---|---|
| WasmCompileException | A module is not valid |
| WasmLinkException | An import is missing or does not match its type |
| WasmTrapException | The code trapped (unreachable, an out-of-bounds access, a division by zero, a stack overflow...) |
| WasmThrowException | The code threw a WebAssembly exception that it did not catch |
| WasmAllocationException | A memory or a table cannot be allocated or grow, or an index is out of bounds |
| WasmTypeMismatchException | A value does not match the type of its destination |
They derive from WasmException.
- TrapKind is StackOverflow when the recursion exceeds MaxStackSize.
- The Exception of a WasmThrowException gives the tag and the values of the WebAssembly exception. A host function throws one with an exception of CreateException: the module can catch it.
- Any other exception of a host function, or of CheckInterrupt, stops the execution and is thrown as is by the call into WebAssembly: WebAssembly cannot catch it.