e01.org

WebAssembly.Exception.prototype.getArg()

The getArg() method of the Exception object can be used to get the value of a specified item in the exception's data arguments.

The **`getArg()`** method of the [`Exception`](/docs/WebAssembly/Reference/JavaScript_interface/Exception) object can be used to get the value of a specified item in the exception's data arguments.

## Syntax

```js-nolint
getArg(exceptionTag, index)
```

### Parameters

- `exceptionTag`
- : A [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag) that must match the tag associated with this exception.
- `index`
- : The index of the value in the data arguments to return.

### Return value

The value of the argument at `index`.

### Exceptions

- TypeError
- : The tags don't match; the exception was not created with the tag passed to the method.
- RangeError
- : The value of the `index` parameter is greater than or equal to the number of fields in the data.

## Description

The `getArg()` method accepts a [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag) as a parameter and will only succeed if the thrown `Exception` was created using the same tag, otherwise it will throw a `TypeError`.
This ensures that the exception can only be read if the calling code has access to the tag.
Tags that are neither imported into nor exported from the WebAssembly code are internal, and their associated exceptions cannot be queried using this method.

> [!NOTE]
> It is not enough that the tag has an identical sequence of data types — it must have the same _identity_ (be the same tag) as was used to create the exception.

## Examples

### Getting exception values from an imported tag

Consider the following WebAssembly code, which is assumed to be compiled to a file called `example.wasm`.
This imports a tag, which it refers to internally as `$tagname`, and exports a method `run` that can be called by external code to throw an exception using the tag.

```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 i32)
local.get 0
throw $tagname
)

;; Exported function "run" that calls $throwException
(func (export "run")
i32.const 1
call $throwException
)
)
```

The code below calls [`WebAssembly.instantiateStreaming`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) to import the `example.wasm` file, passing in an import object (`importObject`) that includes a new [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/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.

```js
const tagToImport = new WebAssembly.Tag({ parameters: ["i32"] });

// Note: the import object properties match the import statement in WebAssembly code!
const importObject = {
extmod: {
exttag: tagToImport,
},
};

WebAssembly.instantiateStreaming(fetch("example.wasm"), importObject)
.then((obj) => {
console.log(obj.instance.exports.run());
})
.catch((e) => {
console.error(e);
console.log(`getArg 0 : ${e.getArg(tagToImport, 0)}`);
});

/* Log output
example.js:40 WebAssembly.Exception: wasm exception
example.js:41 getArg 0 : 1
*/
```

The code catches the exception and uses `getArg()` to print the value at the first index.
In this case, the value is `1`.

### Getting exception values from an exported tag

The process for using an exported tag is very similar to that shown in the previous section.
Here is the same WebAssembly module, simply replacing the import with an export.

```wat
(module
;; Export tag giving it external name: "exptag"
(tag $tagname (export "exptag") (param i32))

(func $throwException (param i32)
local.get 0
throw $tagname
)

(func (export "run")
i32.const 1
call $throwException
)
)
```

The JavaScript is similar too. In this case, we have no imports; instead, we get the exported tag and use that to get the argument.
We also test that we have the right tag using the [`is()` method](/docs/WebAssembly/Reference/JavaScript_interface/Exception/is).

```js
let tagExportedFromWasm;

WebAssembly.instantiateStreaming(fetch("example.wasm"))
.then((obj) => {
// Import the tag using its name from the WebAssembly module
tagExportedFromWasm = obj.instance.exports.exptag;
console.log(obj.instance.exports.run());
})
.catch((e) => {
console.error(e);
// If the tag is correct, get the value
if (e.is(tagExportedFromWasm)) {
console.log(`getArg 0 : ${e.getArg(tagExportedFromWasm, 0)}`);
}
});
```

## Specifications

## Browser compatibility

## See also

- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)

source: MDN Web Docs mirror · page

Cite this

"WebAssembly.Exception.prototype.getArg()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Exception/getArg

JSON record · API collection · citation policy