Node.js new Error() Method - GeeksforGeeks

21 Aug, 2020
For generating and handling errors, Node.js supports so many mechanisms that occur during the running application and it is an inbuilt application, so we don’t import any libraries. All the system errors that are raised by Node.js are inherited from the JavaScript’s <Error> class and provide the least properties available in that class. 
The new Error() method is an inbuilt application programming interface of the Node module in which a new Error object is created and error.message property is set to the provided text message. By calling message.toString(), the text message is generated if an object is passed as a message. Another property error.stack represents the point in the code where new Error() was called, the stack traces of this property are dependent on the V8‘s stack trace API.
Syntax:
new Error(message)
Parameters: This function accepts single parameters as mentioned above and described below:
message <string>:  It accepts the message which is displayed in the console when the error is thrown.
Return Value <Error>: It returns specific errors as defined in message value which is passed as an argument.
Below examples illustrate the use of new Error(message) method in Node.js.
Example 1: Filename: index.js
// Node.js program to demonstrate the // the new Error() method   // Creating new Error along with // passing an error messageconst error = new Error('Error Message: Hey geek, Error is working all fine..');  // Printing error messageconsole.log(error.message);  // Printing error stackconsole.log(error.stack);  // Printing the error in consoleconsole.log(error);
Run index.js file using the following command:
node index.js
Output:
>> Error Message: Hey geek, Error is working all fine..>> Error: Error Message: Hey geek, Error is working all fine..   at Object.<anonymous> (C:\Users\Vikas Kumar\Desktop\test.js:6:15)........at internal/main/run_main_module.js:17:47>> Error: Error Message: Hey geek, Error is working all fine..   at Object.<anonymous> (C:\Users\Vikas Kumar\Desktop\test.js:6:15)........at internal/main/run_main_module.js:17:47
Example 2: Filename: index.js
// Node.js program to demonstrate the // the new Error() method   // Creating new objectconst newObjectError = {};  // Creating Errors along with // passing error messageconst error = new Error('Error Message: Hey Geek, Error is working fine...');  const error1 = new Error();const error2 = new Error();  // Printing the stck trace limitconsole.log(">> ", Error.stackTraceLimit);  // Printing error messageconsole.log(">> ", error.message);  // Printing the error in console// console.log(error1);  // Another way to create error regarding any objectError.captureStackTrace(newObjectError);  // Inspecting the errorconsole.log(">> ", require('util').inspect(error1));  if (error1.stack == newObjectError.stack) {    console.log(">> ", "Different Errors can be compared");} else {    console.log(">> ", "Errors can't be compared");}  // Comparing two equal Errorsif (error1.stack === error2.stack) {    console.log(">> ", "Equal Errors can be compared");} else {    console.log(">> ", "Errors can't be compared");}
Run index.js file using the following command:
node index.js
Output:
>>  10>>  Error Message: Hey Geek, Error is working fine...>>  Error   at Object.<anonymous> (C:\Users\Vikas Kumar\Desktop\test.js:26:16)...........at internal/main/run_main_module.js:17:47>>  Errors can’t be compared>>  Errors can’t be compared
Reference: https://nodejs.org/api/errors.html#errors_new_error_message
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to build a basic CRUD app with Node.js and ReactJS ?
How to connect Node.js with React.js ?
Mongoose Populate() Method
Express.js req.params Property
Mongoose find() Function
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript