JavaScriptMedium
How can you create custom error objects?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To create custom error objects in JavaScript, you can extend the built-in Error class. This allows you to add custom properties and methods to your error objects. Here's a quick example:
js
class CustomError extends Error {
constructor(message) {
super(message);
this.name = "CustomError";
}
}
try {
throw new CustomError("This is a custom error message");
} catch (error) {
console.log(error.name); // CustomError
console.log(error.message); // This is a custom error message
}
