JavaScript - Extending Errors
JavaScript - Extending Errors
Thecustomerrors in JavaScript are errors that you create yourself, as opposed to the built-in errors that JavaScript throws. You can create custom errors to handle specific types of errors that may occur in your code.
To create acustom error, you can use the Error constructor. The Error constructor takes a string as its argument, which will be the message of the error.
Extending the Error Class: Creating Custom Errors
The best way to create custom errors is by creating a new class and extending it using the βextendsβ keyword. It uses the concept of inheritance, and the custom error class inherits the properties of the Error class.
In the constructor() function, you can initialize the properties of the custom error class.
Syntax
You can follow the syntax below to create custom errors by extending the Error class.
class customError extends Error {
constructor(message) {
super(message)
// Initialize properties
}
}In the above code, we call the parent class constructor using the super() method.
You can also initialize the properties of the customError class in the constructor function.
Example
In the code below, we take the input from the user. When a user clicks the check age button, it calls the checkAge() function.
We have defined the ageError class in JavaScript code and extended it with the Error class. In the ageError class, we have added the constructor() function to initialize the properties.
In the constructor() function, we used the super() method to initialize the message property of the Error class. Also, we have initialized the βnameβ and βageβ properties in the constructor function.
In the checkAge() function, we throw the error if the age is less than 18, and in the catch{} block, we print the error message and age.
Output
Enter your age: 5
Check Age
Error: You are too young.
Age: 5If you want to create multiple new classes for the custom errors only to provide the clarified error type and message and donβt want to change the properties of the Error class, you can use the syntax below.
class InputError extends Error {};Letβs learn it via the example below.
Example
In the code below, we have created 3 different custom classes and extended them with the Error class to create custom errors.
In the try{} block, we throw the StringError.
In the catch{} block, we used the instanceOf operator to check the type of the error object and print the error message accordingly.
Output
String ErrorMultilevel Inheritance
You can create a general custom error by extending it with the Error class. After that, you can extend the custom error class to create a more generalized error class.
Letβs understand it via the example below.
Example
In the code below, we have defined the βNotFoundβ class and extended it using the Error class.
After that, we defined the βpropertyNotFoundβ and βvalueNotFoundβ classes and extended them with the βNotFoundβ class. Here, we have done the multilevel inheritance.
In the try block, we throw a valueNotFound error if the array doesnβt contain 6.
In the catch block, we print the error.
Output
propertyNotFound: Array doesn't contain 6You can also throw the propertyNotFound error if any object doesnβt contain the particular property.