[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Error [Back]  [Original]

Error - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Error

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

* Some parts of this feature may have varying levels of support.

Error . Error . .

In this article

Error.

Error , structuredClone() postMessage().

Error, JavaScript , .

EvalError

, , eval().

RangeError

, , .

ReferenceError

, , .

SyntaxError

, , eval().

TypeError

, , .

URIError

, , encodeURI() decodeURI() .

AggregateError

, , . , , Promise.any().

InternalError

, , JavaScript. , (too much recursion).

Error()

Error.

Error.captureStackTrace()

V8, stack Error.

Error.stackTraceLimit

V8, , .

Error.prepareStackTrace()

V8, ( ) JavaScript- V8 , .

Error.prototype Error.

Error.prototype.constructor

-, . Error Error.

Error.prototype.name

. Error.prototype.name "Error". (, TypeError SyntaxError) name.

Error.prototype.stack

, .

Error.

cause

. Error cause .

Mozilla, , .

fileName

Mozilla, , .

lineNumber

Mozilla, , .

message

. Error , .

Error.prototype.toString()

, . Object.prototype.toString().

, Error throw. try...catch:

js
try {
  throw new Error("!");
} catch (e) {
  console.error(`${e.name}: ${e.message}`);
}

instanceof:

js
try {
  foo.bar();
} catch (e) {
  if (e instanceof EvalError) {
    console.error(`${e.name}: ${e.message}`);
  } else if (e instanceof RangeError) {
    console.error(`${e.name}: ${e.message}`);
  }
  //   
  else {
    //       ,    
    throw e;
  }
}

, Error, throw new MyError() instanceof MyError . .

Error JavaScript ? StackOverflow.

: CustomError ES2015.

js
class CustomError extends Error {
  constructor(foo = "bar", ...params) {
    //    (    )  
    super(...params);

    //          (   V8)
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, CustomError);
    }

    this.name = "CustomError";
    //   
    this.foo = foo;
    this.date = new Date();
  }
}

try {
  throw new CustomError("baz", "bazMessage");
} catch (e) {
  console.error(e.name); // CustomError
  console.error(e.foo); // baz
  console.error(e.message); // bazMessage
  console.error(e.stack); // stacktrace
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-error-objects


Web Proxy Viewer  |  New URL  |  Original Page