Throwing error means exception if any exception is not handled then the node server will crash.
The following line throws error:
throw new Error("Some error occurred");
or
var err = new Error("Some error occurred");
throw err;
or
throw "Some error occurred";
The last example (throwing strings) is not good practice and is not recommended (always throw errors which are instances of Error object).
Note that if you throw
an error in your, then the system will crash on that line (if there is no exception handlers), no any code will be executed after that line.
var a = 5;
var err = new Error("Some error message");
throw err; //this will print the error stack and node server will stop
a++; //this line will never be executed
console.log(a); //and this one also
But in this example:
var a = 5;
var err = new Error("Some error message");
console.log(err); //this will print the error stack
a++;
console.log(a); //this line will be executed and will print 6