| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Error | [Back] [Original] |
Get to know MDN better
Error
Error structuredClone() postMessage()
EvalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIErrorAggregateErrorInternalError "too much recursion" () JavaScript
Error() Error
Error.captureStackTrace()Error.isError() true false
Error.prepareStackTrace() JavaScript V8 Stack Trace API
Error.prototype Error
Error.prototype.constructor Error Error
Error.prototype.nameError.prototype.name "Error" TypeError SyntaxError name
Error.prototype.stack Error
cause Error cause
columnNumber Mozilla
fileName Mozilla
lineNumber Mozilla
message Error
Error throw try...catch
try {
throw new Error("Whoops!");
} catch (e) {
console.error(`${e.name}: ${e.message}`);
}
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 {
// Error
throw e;
}
}
Error
Error options cause try/catch
2 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;
}
}
: Error cause cause
super() options cause Error() options.cause cause
class MyError extends Error {
constructor(message, options) {
// "cause" `options`
super(message, options);
}
}
console.log(new MyError("test", { cause: new Error("cause") }).cause);
// Error: cause
Error throw new MyError() instanceof MyError
Stack Overflow "What's a good way to extend Error in JavaScript?"
:
ES6 Reflect.construct() new.target Object.setPrototypeOf(this, CustomError.prototype) CustomError the TypeScript FAQ
:
ES2015 CustomError
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); // stack trace
}
| ECMAScript 2027 LanguageSpecification # sec-error-objects |
| Web Proxy Viewer | New URL | Original Page |