Skip to content
Related Articles

Related Articles

Node.js new Error() Method
  • Last Updated : 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 message
const error = new Error(
'Error Message: Hey geek, Error is working all fine..');
  
// Printing error message
console.log(error.message);
  
// Printing error stack
console.log(error.stack);
  
// Printing the error in console
console.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 object
const newObjectError = {};
  
// Creating Errors along with 
// passing error message
const error = new Error(
'Error Message: Hey Geek, Error is working fine...');
  
const error1 = new Error();
const error2 = new Error();
  
// Printing the stck trace limit
console.log(">> ", Error.stackTraceLimit);
  
// Printing error message
console.log(">> ", error.message);
  
// Printing the error in console
// console.log(error1);
  
// Another way to create error regarding any object
Error.captureStackTrace(newObjectError);
  
// Inspecting the error
console.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 Errors
if (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

My Personal Notes arrow_drop_up
Recommended Articles
Page :