Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2022.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Web Workers.
The WebAssembly.Exception object represents a runtime exception thrown in a Wasm module.
Creates a new WebAssembly.Exception object instance.
Tests whether the exception matches a particular tag.
Exception.prototype.getArg()Returns the data fields of an exception that matches a specified tag.
Returns the stack trace for the exception.
When handling Wasm exceptions from the JavaScript host, caught exceptions will have a WebAssembly.Exception object type.
For example, you could start by constructing an error tag type using the WebAssembly.Tag() constructor, like this:
You can then import it into a Wasm module like this:
You could then try running an exported Wasm function in a try...catch statement. If the function throws, the error propagated to the catch block will be a WebAssembly.Exception object instance.
You can check whether it has the same exception type we defined earlier (myErrorTag) using Exception.prototype.is(), and then access the exception's payload using Exception.prototype.getArg()).
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, passing options.traceStack=true parameter in the constructor. The constructor may then return an exception with a stack trace attached to the stack property.
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 file example.wasm.
The code below calls WebAssembly.instantiateStreaming to import the example.wasm file, passing in an "import object" (importObject) that includes a new WebAssembly.Tag named tagToImport. The import object defines an object with properties that match the import statement in the WebAssembly code.
Once the file is instantiated, the code calls the exported WebAssembly run() method, which will immediately throw an exception.
The exception is caught in JavaScript using the catch block. We can see it is of type WebAssembly.Exception, but if we didn't have the right tag we couldn't do much else.
However, because we have a tag, we use Exception.prototype.is() to check that it's the right one, and because it is correct, we call Exception.prototype.getArg() to read the value of "42".
| WebAssembly JavaScript Interface: Exception Handling # runtime-exceptions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on May 22, 2026 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.