Exceptions and traps

edit
using 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);
ExceptionThrown when
WasmCompileExceptionA module is not valid
WasmLinkExceptionAn import is missing or does not match its type
WasmTrapExceptionThe code trapped (unreachable, an out-of-bounds access, a division by zero, a stack overflow...)
WasmThrowExceptionThe code threw a WebAssembly exception that it did not catch
WasmAllocationExceptionA memory or a table cannot be allocated or grow, or an index is out of bounds
WasmTypeMismatchExceptionA value does not match the type of its destination

They derive from WasmException.