JS Coding Questions Logo
JS Coding Questions
#339💼 Interview💻 Code

What is the purpose of Error object

Advertisement

728x90

The Error constructor creates an error object and the instances of error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. The syntax of error object would be as below,

javascript
1new Error([message[, fileName[, lineNumber]]])

You can throw user defined exceptions or errors using Error object in try...catch block as below,

javascript
1try {
2  if (withdraw > balance)
3  throw new Error("Oops! You don't have enough balance");
4  } catch (e) {
5  console.log(e.name + ": " + e.message);
6  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 82

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
339of476