- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Custom Errors
JavaScript Errors
JavaScript Custom Errors
A custom error class describes a failure specific to your own program.
Extend the built-in Error class and give it a name.
Then the caller can tell your failures apart from everything else.
Example
Example
javascript
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
let name;
try {
throw new ValidationError("name is required");
} catch (error) {
name = error.name;
}
console.log(name);