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

Error - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Error

*

20157

*

Error Error

Error

Error JavaScript

EvalError

error eval()

RangeError

error

ReferenceError

error

SyntaxError

error

TypeError

error

URIError

error encodeURI() decodeURI()

AggregateError

error Promise.any()

InternalError

Javascript

Error()

Error

Error.captureStackTrace()

V8 Error stack

Error.stackTraceLimit

V8

Error.prepareStackTrace()

V8 V8 JavaScript

Error.prototype.message

Error

Error.prototype.name

Error.prototype.cause

Error

Error.prototype.fileName

Mozilla

Error.prototype.lineNumber

Mozilla

Error.prototype.columnNumber

Mozilla

Error.prototype.stack

Error.prototype.toString()

Object.prototype.toString()

throw Error try...catch

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

constructor JavaScript 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);
  }
  // ... etc
  else {
    // If none of our cases matched leave the Error unhandled
    throw e;
  }
}

Error option cause try/catch

doFailSomeWay() doFailAnotherWay()

js
function doWork() {
  try {
    doFailSomeWay();
  } catch (err) {
    throw new Error("Failed in some way", { cause: err });
  }
  try {
    doFailAnotherWay();
  } catch (err) {
    throw new Error("Failed in another way", { cause: err });
  }
}

try {
  doWork();
} catch (err) {
  switch (err.message) {
    case "Failed in some way":
      handleFailSomeWay(err.cause);
      break;
    case "Failed in another way":
      handleFailAnotherWay(err.cause);
      break;
  }
}

cause super() options

js
class MyError extends Error {
  constructor(/* some arguments */) {
    // Needs to pass both `message` and `options` to install the "cause" property.
    super(message, options);
  }
}

Error throw new MyError() instanceof MyError

StackOverflow What's a good way to extend Error in JavaScript?

ES6

Babel 7 CustomError Object.defineProperty() Babel

ES2015 CustomError

js
class CustomError extends Error {
  constructor(foo = "bar", ...params) {
    // Pass remaining arguments (including vendor specific ones) to parent constructor
    super(...params);

    // Maintains proper stack trace for where our error was thrown (only available on V8)
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, CustomError);
    }

    this.name = "CustomError";
    // Custom debugging information
    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
}

ES5

CustomError

js
function CustomError(foo, message, fileName, lineNumber) {
  var instance = new Error(message, fileName, lineNumber);
  instance.foo = foo;
  Object.setPrototypeOf(instance, CustomError.prototype);
  if (Error.captureStackTrace) {
    Error.captureStackTrace(instance, CustomError);
  }
  return instance;
}

Object.setPrototypeOf(CustomError.prototype, Error.prototype);

Object.setPrototypeOf(CustomError, Error);

CustomError.prototype.name = "CustomError";

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

ECMAScript 2027 LanguageSpecification
# sec-error-objects


Web Proxy Viewer  |  New URL  |  Original Page