Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WebAssembly.Exception

BaselineWidely available *

Note: This feature is available inWeb Workers.

TheWebAssembly.Exception object represents a runtime exception thrown from WebAssembly to JavaScript, or thrown from JavaScript to a WebAssembly exception handler.

Theconstructor accepts aWebAssembly.Tag, an array of values, and anoptions object as arguments.The tag uniquely defines thetype of an exception, including the order of its arguments and their data types.The same tag that was used to create theException is required to access the arguments of a thrown exception.Methods are provided to test whether an exception matches a particular tag, and also to get a particular value by index (if the exception matches specified tag).

JavaScript and other client code can only access WebAssembly exception values, and vice versa, when the associated tag is shared (you can't just use another tag that happens to define the same data types).Without the matching tag, exceptions can be caught and re-thrown, but they can't be inspected.

In order to make exception-throwing faster, exceptions thrown from WebAssembly generally do not include a stack trace.WebAssembly code that needs to provide a stack trace must call a JavaScript function to create the exception, passingoptions.traceStack=true parameter in the constructor.The constructor may then return an exception with a stack trace attached to thestack property.

Constructor

WebAssembly.Exception()

Creates a newWebAssembly.Exception object.

Instance methods

Exception.prototype.is()

Tests whether the exception matches a particular tag.

Exception.prototype.getArg()

Returns the data fields of an exception that matches a specified tag.

Instance properties

Examples

This example shows how to define a tag and import it into a module, then use it to throw an exception that is caught in JavaScript.

Consider the following WebAssembly code, which is assumed to be compiled to a fileexample.wasm.

  • The module imports a tag that is referred to as$tagname internally and that has a singlei32 parameter.The tag expects the tag to be passed using moduleextmod and tagexttag.
  • The$throwException function throws an exception using thethrow instruction, taking the$tagname and the parameter argument.
  • The module exports the functionrun() that throws an exception with the value "42".
wat
(module  ;; import tag that will be referred to here as $tagname  (import "extmod" "exttag" (tag $tagname (param i32)))  ;; $throwException function throws i32 param as a $tagname exception  (func $throwException (param $errorValueArg i32)    local.get $errorValueArg    throw $tagname  )  ;; Exported function "run" that calls $throwException  (func (export "run")    i32.const 42    call $throwException  ))

The code below callsWebAssembly.instantiateStreaming to import theexample.wasm file, passing in an "import object" (importObject) that includes a newWebAssembly.Tag namedtagToImport.The import object defines an object with properties that match theimport statement in the WebAssembly code.

Once the file is instantiated, the code calls the exported WebAssemblyrun() method, which will immediately throw an exception.

js
const tagToImport = new WebAssembly.Tag({ parameters: ["i32"] });// Note: import object properties match the WebAssembly import statement!const importObject = {  extmod: {    exttag: tagToImport,  },};WebAssembly.instantiateStreaming(fetch("example.wasm"), importObject)  .then((obj) => {    console.log(obj.instance.exports.run());  })  .catch((e) => {    console.error(e);    // Check we have the right tag for the exception    // If so, use getArg() to inspect it    if (e.is(tagToImport)) {      console.log(`getArg 0 : ${e.getArg(tagToImport, 0)}`);    }  });/* Log outputexample.js:40 WebAssembly.Exception: wasm exceptionexample.js:41 getArg 0 : 42*/

The exception is caught in JavaScript using thecatch block.We can see it is of typeWebAssembly.Exception, but if we didn't have the right tag we couldn't do much else.

However, because we have a tag, we useException.prototype.is() to check that it's the right one, and because it is correct, we callException.prototype.getArg() to read the value of "42".

Specifications

Specification
WebAssembly JavaScript Interface: Exception Handling
# runtime-exceptions

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp