[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/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 7.

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

Error . Error . .

In this article

Error .

JavaScript Error . .

EvalError

eval() .

RangeError

.

ReferenceError

.

SyntaxError

eval() .

TypeError

.

URIError

encodeURI() decodeURl() .

AggregateError

(: Promise.any()) .

InternalError

JavaScript .

Error()

Error .

Error.captureStackTrace()

stack V8

Error.prototype.message

Error.prototype.name

Error.prototype.description

. message .

Error.prototype.number

Error.prototype.fileName

Mozilla

Error.prototype.lineNumber

Mozilla

Error.prototype.columnNumber

Mozilla

Error.prototype.stack

Mozilla

Error.prototype.toString()

. Object.prototype.toString() ..

Error throw . try...catch .

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

constructor , . Javascript instanceof .

js
try {
  foo.bar();
} catch (e) {
  if (e instanceof EvalError) {
    alert(e.name + ": " + e.message);
  } else if (e instanceof RangeError) {
    alert(e.name + ": " + e.message);
  }
  // ... etc
}

throw new MyError() instanceof MyError Error . . StackOverflow "What's a good way to extend Error in JavaScript?" .

ES6

: Babel 7 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);
    }

    // Custom debugging information
    this.foo = foo;
    this.date = new Date();
  }
}

try {
  throw new CustomError("baz", "bazMessage");
} catch (e) {
  console.log(e.foo); //baz
  console.log(e.message); //bazMessage
  console.log(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, Object.getPrototypeOf(this));
  if (Error.captureStackTrace) {
    Error.captureStackTrace(instance, CustomError);
  }
  return instance;
}

CustomError.prototype = Object.create(Error.prototype, {
  constructor: {
    value: Error,
    enumerable: false,
    writable: true,
    configurable: true,
  },
});

if (Object.setPrototypeOf) {
  Object.setPrototypeOf(CustomError, Error);
} else {
  CustomError.__proto__ = Error;
}

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-error-objects


Web Proxy Viewer  |  New URL  |  Original Page