| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Error | [Back] [Original] |
Get to know MDN better
Error
EvalError error eval()
RangeErrorerror
ReferenceErrorerror
SyntaxErrorerror
TypeErrorerror
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.nameError.prototype.cause Error
Error.prototype.fileName Mozilla
Error.prototype.lineNumber Mozilla
Error.prototype.columnNumber Mozilla
Error.prototype.stack throw Error try...catch
try {
throw new Error("Whoops!");
} catch (e) {
console.error(e.name + ": " + e.message);
}
constructor JavaScript instanceof
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()
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
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?
Babel 7 CustomError 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);
}
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
}
CustomError
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 |
core-js cause Error polyfillthrowtry...catchError.captureStackTrace()Error.stackTraceLimit Error.prepareStackTrace() V8 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()AggregateErrorEvalErrorInternalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIError| Web Proxy Viewer | New URL | Original Page |