javascript create custom error
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!");
Test your knowledge with interactive quizzes.
Prepare for interviews with curated question sets.
Ask your coding-related doubts and get answers.
Earn certifications to enhance your resume.
Hands-on projects to improve your skills.
Test your knowledge with interactive quizzes.
Prepare for interviews with curated question sets.
Add your technical blogs and read technical topics.
Earn certifications to enhance your resume.
Hands-on projects to improve your skills.
People Also Asked |
---|