Error Object in JS
JavaScript has a built in error object that provides error information when an error occurs. Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. The Error object provides error information when an error occurs.
Error is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage().
The Error object has many properties and methods including:
- name: The name of the error, or more specifically, the name of the constructor function the error belongs to.
- message: A human-readable description of the error. Error message. For user-created
Errorobjects, this is the string provided as the constructor's first argument. - stack: A non-standard property for a stack trace.
- toString(): Returns a string representing the specified object. Overrides the
Object.prototype.toString()method.
Error types
Besides the generic Error constructor, there are other core error constructors in JavaScript.
EvalError Creates an instance representing an error that occurs regarding the global function eval().
RangeError Creates an instance representing an error that occurs when a numeric variable or parameter is outside its valid range.
ReferenceError Creates an instance representing an error that occurs when de-referencing an invalid reference.
Get Deepak Kumar Singh’s stories in your inbox
Join Medium for free to get updates from this writer.
SyntaxError Creates an instance representing a syntax error.
TypeError Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.
URIError Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.
Create an error
The Error() constructor creates Error objects. Error() can be called with or without new. Both create a new Error instance. The first argument is the message.
let err1 = Error("Created using a function call!");
let err2 = new Error("Created via the new keyword!");
console.log(err1);
console.log(err2);Output:
Error: Created using a function call!
at Object.<anonymous> (.../test-error.js:1:14)
at Module._compile (node:internal/modules/cjs/loader:1198:14)
at Object.Module._extensions..js (node:.../modules/cjs/loader:1252:10)
at Module.load (node:internal/modules/cjs/loader:1076:32)
at Function.Module._load (node:internal/modules/cjs/loader:911:12)
at Function.executeUserEntryPoint [as runMain] (node:../run_main:81:12)
at node:internal/main/run_main_module:22:47
Error: Created via the new keyword!
at Object.<anonymous> (.../test-error.js:2:14)
at Module._compile (node:internal/modules/cjs/loader:1198:14)
at Object.Module._extensions..js (node:.../modules/cjs/loader:1252:10)
at Module.load (node:internal/modules/cjs/loader:1076:32)
at Function.Module._load (node:internal/modules/cjs/loader:911:12)
at Function.executeUserEntryPoint [as runMain] (node:../run_main:81:12)
at node:internal/main/run_main_module:22:47Another example:
let err1 = new Error("A new error occured!");
console.log(err1.name);
console.log(err1.message);
console.log(err1.stack);
console.log(err1.toString());Output:
Error
A new error occured!
Error: A new error occured!
at Object.<anonymous> (.../test-error.js:1:14)
at Module._compile (node:internal/modules/cjs/loader:1198:14)
at Object.Module._extensions..js (node:../modules/cjs/loader:1252:10)
at Module.load (node:internal/modules/cjs/loader:1076:32)
at Function.Module._load (node:internal/modules/cjs/loader:911:12)
at Function.executeUserEntryPoint [as runMain] (node:../run_main:81:12)
at node:internal/main/run_main_module:22:47
Error: A new error occured!Next, we see error handling.






