throw
Throws an exception for JavaScript to catch. This can only throw imported (tag...) objects. These are used to declare the parameters that will be sent with the exception.

Syntax

throw $tagFunction

Parameters

$tagFunction
The name of the tag function that will be thrown.

Stack In

* Any of the parameters the exception tag function requires.

Stack Out

NONE

Example

;; Import exception (used with a single i32 value)
(import "import" "integerException" (tag $integerException (param i32)))

;; Create function that will throw the exception
(func (export "throwIntegerException")
  ;; Push the exception parameters on to the stack
  i32.const 101

  ;; Then throw the tag/exception
  throw $integerException
)
try {
  // Call the function that will throw the exception
  wasm.instance.exports.throwIntegerException();
} catch (e) {
  // Check it is a Web Assembly exception
  if (e instanceof WebAssembly.Exception) {
    // Check it is the right tag
    if (e.is(tagIntegerException) === true) {
      // Get the arguments
      const value = e.getArg(tagIntegerException, 0);

      // Log result
      console.log('IntegerException thrown value = ' + value);
    } else {
      // Log error
      console.log('Unknown WebAssembly.Exception');
    }
  } else {
    // Log error
    console.log('Unknown exception');
  }
}