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

瀏覽器相容性

另見