WebAssembly.RuntimeError() 构造函数

Baseline 已广泛支持

此功能已非常成熟,可在多种设备和浏览器版本上使用。自 2017 年 10 月以来,它已在各大浏览器中可用。

WebAssembly.RuntimeError() 构造函数创建一个新的 WebAssembly RuntimeError 对象——每当 WebAssembly 指定 trap 时抛出的类型。

语法

js
new WebAssembly.RuntimeError()
new WebAssembly.RuntimeError(message)
new WebAssembly.RuntimeError(message, options)
new WebAssembly.RuntimeError(message, fileName)
new WebAssembly.RuntimeError(message, fileName, lineNumber)

参数

message 可选

错误的易于人类理解的描述。

options 可选

一个具有以下属性的对象

cause 可选

一个指示错误具体原因的属性。在捕获并重新抛出具有更具体或有用错误消息的错误时,可以使用此属性来传递原始错误。

fileName 可选 非标准

包含导致异常的代码的文件的名称。

lineNumber 可选 非标准

导致异常的代码的行号。

示例

创建新的 RuntimeError 实例

以下代码片段创建了一个新的 RuntimeError 实例,并将其详细信息记录到控制台。

js
try {
  throw new WebAssembly.RuntimeError("Hello", "someFile", 10);
} catch (e) {
  console.log(e instanceof WebAssembly.RuntimeError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "RuntimeError"
  console.log(e.fileName); // "someFile"
  console.log(e.lineNumber); // 10
  console.log(e.columnNumber); // 0
  console.log(e.stack); // returns the location where the code was run
}

规范

规范
WebAssembly JavaScript 接口
# exceptiondef-runtimeerror
ECMAScript® 2026 语言规范
# sec-nativeerror-constructors

浏览器兼容性

另见