Throwing Error
suggest changethrow raises an exception. If nothing catches it, the process crashes.
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).
If nothing catches the exception, Node stops on that line. Later lines in the same turn do not run.
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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents