Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does throwing Exception kills thread in node js?

Tags:

node.js

as I know node js is single threaded, so if I write

throw new Error()

somewhere in method, it should kill the current thread, so will shut down the whole process?

like image 213
onik Avatar asked Feb 22 '16 12:02

onik


1 Answers

Reading the NodeJs documentation about exceptions helps you understand quite easily what happens.

On the section Error Propagation and Interception you can read the following:

Node.js supports several mechanisms for propagating and handling errors that occur while an application is running. How these errors are reported and handled depends entirely on the type of Error and the style of the API that is called.

And answering to your question:

Any use of the JavaScript throw mechanism will raise an exception that must be handled using try / catch or the Node.js process will exit immediately.

So basically any unhandled exception will kill the whole node process.

That's where tools like PM2 kick in, restarting your node application whenever that happens.

Although this is from 2012 I think it's a nice article and quite simple to understand what should you do with uncaught exceptions

like image 162
Fabio Antunes Avatar answered Sep 30 '22 14:09

Fabio Antunes