InterviewHubPRO
Section Overview
Q343

Question 343 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

66%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

What is the purpose of Error object

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
  new Error([message[, fileName[, lineNumber]]])

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

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