WebAssembly.RuntimeError

Baseline 已广泛支持

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

WebAssembly.RuntimeError 对象是 WebAssembly 指定 trap 时抛出的错误类型。

构造函数

WebAssembly.RuntimeError()

创建一个新的 WebAssembly.RuntimeError 对象。

实例属性

WebAssembly.RuntimeError.prototype.message

错误消息。从 Error 继承。

WebAssembly.RuntimeError.prototype.name

错误名称。从 Error 继承。

WebAssembly.RuntimeError.prototype.cause

错误原因。从 Error 继承。

WebAssembly.RuntimeError.prototype.fileName 非标准

引发此错误的文件的路径。从 Error 继承。

WebAssembly.RuntimeError.prototype.lineNumber 非标准

引发此错误的行号。从 Error 继承。

WebAssembly.RuntimeError.prototype.columnNumber 非标准

引发此错误的行中的列号。从 Error 继承。

WebAssembly.RuntimeError.prototype.stack 非标准

堆栈跟踪。从 Error 继承。

实例方法

WebAssembly.RuntimeError.prototype.toString()

返回表示指定 Error 对象的字符串。从 Error 继承。

示例

创建新的 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-native-error-types-used-in-this-standard

浏览器兼容性

另见