大网数据广告活动
Web 标准与前端开发

SuppressedError

· 52CE 我爱测

The **`SuppressedError`** object represents an error generated while handing another error. It is generated during resource disposal using Statements/using or Statements/await_using.

The SuppressedError object represents an error generated while handing another error. It is generated during resource disposal using Statements/using or Statements/await_using.

Compared to AggregateError, SuppressedError represents an error that happened during the handling of another error, while AggregateError represents a list of unrelated errors. It is possible, though, for a SuppressedError to contain a chain of suppressed errors (e.suppressed.suppressed.suppressed...). It is also semantically different from Error/cause because the error is not caused by another error, but happens when handling another error.

SuppressedError is a subclass of Error.

Constructor

  • SuppressedError/SuppressedError
    • : Creates a new SuppressedError object.

Instance properties

Also inherits instance properties from its parent Error.

These properties are defined on SuppressedError.prototype and shared by all SuppressedError instances.

  • Object/constructor
    • : The constructor function that created the instance object. For SuppressedError instances, the initial value is the SuppressedError/SuppressedError constructor.
  • Error/name
    • : Represents the name for the type of error. For SuppressedError.prototype.name, the initial value is "SuppressedError".

[!NOTE] SuppressedError never has the Error/cause property, because the semantics of cause overlaps with suppressed.

These properties are own properties of each SuppressedError instance.

  • SuppressedError/error
    • : A reference to the error that results in the suppression.
  • SuppressedError/suppressed
    • : A reference to the error that is suppressed by error.

Instance methods

Inherits instance methods from its parent Error.

Examples

Catching a SuppressedError

A SuppressedError is thrown when an error occurs during resource disposal. Throwing an error causes scope cleanup, and each disposer during the cleanup can throw its own error. All these errors are collected into a chain of SuppressedError instances, with the original error as the suppressed property and the new error thrown by the next disposer as the error property.

try {
  using resource1 = {
    [Symbol.dispose]() {
      throw new Error("resource1 disposal failed");
    },
  };
  using resource2 = {
    [Symbol.dispose]() {
      throw new Error("resource2 disposal failed");
    },
  };
  throw new TypeError("Original error");
} catch (e) {
  console.log(e instanceof SuppressedError); // true
  console.log(e.message); // "An error was suppressed during disposal"
  console.log(e.name); // "SuppressedError"
  console.log(e.error); // Error: resource1 disposal failed
  console.log(e.suppressed); // SuppressedError: An error was suppressed during disposal
  console.log(e.suppressed.error); // Error: resource2 disposal failed
  console.log(e.suppressed.suppressed); // TypeError: Original error
}

The chain looks like this:

     SuppressedError --suppressed--> SuppressedError --suppressed--> TypeError
            |                               |
          error                           error
            |                               |
            v                               v
resource1 disposal failed       resource2 disposal failed
 (Disposal happens later)       (Disposal happens earlier)

Creating a SuppressedError

try {
  throw new SuppressedError(
    new Error("New error"),
    new Error("Original error"),
    "Hello",
  );
} catch (e) {
  console.log(e instanceof SuppressedError); // true
  console.log(e.message); // "Hello"
  console.log(e.name); // "SuppressedError"
  console.log(e.error); // Error: "New error"
  console.log(e.suppressed); // Error: "Original error"
}

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。