| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
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 .
.
Error .
EvalError eval()
.
RangeError.
ReferenceError.
SyntaxErroreval()
.
TypeError.
URIErrorAggregateError (:
Promise.any())
.
InternalError JavaScript .
Error() Error .
Error.prototype.message
Error.prototype.name
Error.prototype.description .
message .
Error.prototype.number
Error.prototype.fileNameMozilla
Error.prototype.lineNumberMozilla
Error.prototype.columnNumberMozilla
Error.prototype.stackMozilla
Error throw . try...catch .
try {
throw new Error("!");
} catch (e) {
alert(e.name + ": " + e.message);
}
constructor
, . Javascript
instanceof .
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?" .
:
Babel 7 Object.defineProperty() . Babel .
:
ES2015 , CustomError .
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
}
CustomError
.
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 |
This page was last modified on 2026 9 28 by MDN contributors.
ErrorObject/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()AggregateErrorEvalErrorInternalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIErrorYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |