Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Error() constructor

BaselineWidely available

TheError() constructor createsError objects.

Syntax

js
new Error()new Error(message)new Error(message, options)new Error(message, fileName)new Error(message, fileName, lineNumber)Error()Error(message)Error(message, options)Error(message, fileName)Error(message, fileName, lineNumber)

Note:Error() can be called with or withoutnew. Both create a newError instance.

Parameters

messageOptional

A human-readable description of the error.

optionsOptional

An object that has the following properties:

causeOptional

A value indicating the specific cause of the error, reflected in thecause property. When catching and re-throwing an error with a more-specific or useful error message, this property can be used to pass the original error.

fileNameOptionalNon-standard

The path to the file that raised this error, reflected in thefileName property. Defaults to the name of the file containing the code that called theError() constructor.

lineNumberOptionalNon-standard

The line number within the file on which the error was raised, reflected in thelineNumber property. Defaults to the line number containing theError() constructor invocation.

Examples

Function call or new construction

WhenError is used like a function, that is withoutnew, it will return anError object.Therefore, a mere call toError will produce the same output that constructing anError object via thenew keyword would.

js
const x = Error("I was created using a function call!");// above has the same functionality as followingconst y = new Error('I was constructed via the "new" keyword!');

Rethrowing an error with a cause

It is sometimes useful to catch an error and re-throw it with a new message.In this case you should pass the original error into the constructor for the newError, as shown.

js
try {  frameworkThatCanThrow();} catch (err) {  throw new Error("New error message", { cause: err });}

For a more detailed example seeError > Differentiate between similar errors.

Omitting options argument

JavaScript only tries to readoptions.cause ifoptions is an object — this avoids ambiguity with the other non-standardError(message, fileName, lineNumber) signature, which requires the second parameter to be a string. If you omitoptions, pass a primitive value asoptions, or pass an object without thecause property, then the createdError object will have nocause property.

js
// Omitting optionsconst error1 = new Error("Error message");console.log("cause" in error1); // false// Passing a primitive valueconst error2 = new Error("Error message", "");console.log("cause" in error2); // false// Passing an object without a cause propertyconst error3 = new Error("Error message", { details: "http error" });console.log("cause" in error3); // false

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-error-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp