- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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>
- Related Articles
- Explain 'Not a constructor function' error in JavaScript?
- What is function() constructor in JavaScript?
- What is a constructor method in JavaScript?
- What is a default constructor in JavaScript?
- What is a constructor to create String object in JavaScript?
- What is JSlint error "missing radix parameter" in JavaScript?
- What is the difference between new operator and object() constructor in JavaScript?
- JavaScript Boolean constructor Property
- JavaScript Date constructor Property
- JavaScript Array prototype Constructor
- What is Error Detection?
- What is Error Correction?
- What is Parallax error ?
- What is the difference between void, eval, and the Function constructor in JavaScript?
- What is constructor overloading in Java?
