Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. SuppressedError

SuppressedError

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

TheSuppressedError object represents an error generated while handing another error. It is generated during resource disposal usingusing orawait using.

Compared toAggregateError,SuppressedError is used to represent a single error that is suppressed by another error, whileAggregateError represents a list of unrelated errors. It is possible, though, for aSuppressedError to contain a chain of suppressed errors (e.suppressed.suppressed.suppressed...). It is also semantically different fromcause because the error is notcaused by another error, buthappens when handling another error.

SuppressedError is a subclass ofError.

Constructor

SuppressedError()

Creates a newSuppressedError object.

Instance properties

Also inherits instance properties from its parentError.

These properties are defined onSuppressedError.prototype and shared by allSuppressedError instances.

SuppressedError.prototype.constructor

The constructor function that created the instance object. ForSuppressedError instances, the initial value is theSuppressedError constructor.

SuppressedError.prototype.name

Represents the name for the type of error. ForSuppressedError.prototype.name, the initial value is"SuppressedError".

Note:SuppressedError never has thecause property, because the semantics ofcause overlaps withsuppressed.

These properties are own properties of eachSuppressedError instance.

error

A reference to the error that results in the suppression.

suppressed

A reference to the error that is suppressed byerror.

Instance methods

Inherits instance methods from its parentError.

Examples

Catching an SuppressedError

ASuppressedError is thrown when an error occurs duringresource 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 ofSuppressedError instances, with the original error as thesuppressed property and the new error thrown by the next disposer as theerror property.

js
try {  using resource1 = {    [Symbol.dispose]() {      throw new Error("Error while disposing resource1");    },  };  using resource2 = {    [Symbol.dispose]() {      throw new Error("Error while disposing resource2");    },  };  throw new Error("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: Error while disposing resource1  console.log(e.suppressed); // SuppressedError: An error was suppressed during disposal  console.log(e.suppressed.error); // Error: Error while disposing resource2  console.log(e.suppressed.suppressed); // Error: Original error}

The chain looks like this:

       SuppressedError --suppressed--> SuppressedError --suppressed--> Original error              |                               |            error                           error              v                               vError while disposing resource1   Error while disposing resource2    (Disposal happens later)        (Disposal happens earlier)

Creating an SuppressedError

js
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

Specification
ECMAScript Async Explicit Resource Management
# sec-suppressederror-objects

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp