What is JavaScript Error Constructor?


A JavaScript constructor is a function that creates and initializes an object instance of a class. A constructor is used to create a new object and set values for existing object properties. The Error() constructor in JavaScript is used to create new error objects. Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types.

Syntax

Following is the syntax for an Error( ) constructor −

new Error()
new Error(message)
new Error(message, options)
new Error(message, fileName)
new Error(message, fileName, lineNumber)

The Error() constructor can be defined with different-different parameter and each parameter has its own meaning as defined below −

  • message − It is an optional parameter A human-readable description of this error object.  An error message can be set using the JavaScript error message property.


  • options − It is an optional parameter a property indicating the specific reason which the error occurs. When catching and re-throwing an error with a more-specific or useful error message, this property should be used to pass the original error.

  • fileName − It is an optional parameter having value for the fileName property on the created Error object. If no name is provided then fileName is equal to the name of file containing the code that is called the Error() constructor.

  • lineNumber − It is an optional parameter The value for the lineNumber property on the created Error object.  If no number is provided then the lineNumber is equal to the line number containing the Error() constructor.

There are two options using which we can create an error object one is by using a function call and the other is by using a new keyword.

// Using Function Call
const x = Error(''This error constructor is created using function call!')
// Using new Keyword
const y = new Error(''This object is created using "new" keyword!')

Example

Creating Error using Function Call

We use Error like a function without a new keyword. When the Error is used as a function it will return an error object the same as the error object created using the new keyword. We can use the below program to create an error object using a function call. In this program, we created an error object and throw it using the throw keyword

<html>
<body>
   <h3> Create Error Using Function Call</h3>
   <p id = "result"> </p>
   <script>
      const err = Error("This error is created using function call");
      try{
         throw err;
      }
      catch(e){
         document.getElementById("result").innerHTML = e;
      }
   </script>
</body>
</html>

Example (Creating Error using new Keyword)

We can create an error object using the keyword ‘new’. We can use the below program to create an error object using new keyword. We throw the error using try…catch and throw.

<html>
<body>
   <p id = "result"> </p>
   <script>
      const err = new Error("This error object is created using new keyword");
      try{
         throw err;
      }
      catch(e){
      document.getElementById("result").innerHTML = e;
   }
   </script>
</body>
</html>

Updated on: 21-Jul-2022

929 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements