Waiting Answer November 05, 2023

How to Create Custom Error in JavaScript?

Answers
2023-12-01 11:56:18

class CustomError extends Error {
  constructor(message) {
    super(message);
    this.name = "CustomError";
  }
}

 

  In the above example, we create a new class called CustomError that extends the built-in Error class. We then define a constructor function that takes a message parameter and passes it to the super() method, which calls the parent constructor. We also set the name property of the error to "CustomError".

Once you have defined your custom error, you can throw it like any other error:

           throw new CustomError("Something went wrong!");
 

Your Answer

Recently Asked Questions

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.

Python has a variety of libraries such as NumPy, pandas, and matplotlib that make it an ideal language for data analysis and visualization.

Java is commonly used for building enterprise-scale applications.