抛出异常会杀死节点js中的线程吗?

时间:2022-07-25 20:59:45

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

因为我知道节点js是单线程的,所以如果我写

throw new Error()

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

在某个方法中,它应该杀死当前线程,所以会关闭整个进程吗?

1 个解决方案

#1


5  

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

阅读有关异常的NodeJs文档可以帮助您轻松理解发生的情况。

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

在Error Propagation和Interception部分,您可以阅读以下内容:

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.

Node.js支持多种机制来传播和处理应用程序运行时发生的错误。如何报告和处理这些错误完全取决于Error的类型和调用的API的样式。

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.

任何使用JavaScript throw机制都会引发必须使用try / catch处理的异常,否则Node.js进程将立即退出。

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.

这就是像PM2这样的工具启动的地方,每当发生这种情况时重启节点应用程序。

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

虽然这是从2012年开始的,但我认为它是一篇很好的文章,很容易理解你应该如何处理未捕获的异常

#1


5  

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

阅读有关异常的NodeJs文档可以帮助您轻松理解发生的情况。

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

在Error Propagation和Interception部分,您可以阅读以下内容:

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.

Node.js支持多种机制来传播和处理应用程序运行时发生的错误。如何报告和处理这些错误完全取决于Error的类型和调用的API的样式。

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.

任何使用JavaScript throw机制都会引发必须使用try / catch处理的异常,否则Node.js进程将立即退出。

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.

这就是像PM2这样的工具启动的地方,每当发生这种情况时重启节点应用程序。

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

虽然这是从2012年开始的,但我认为它是一篇很好的文章,很容易理解你应该如何处理未捕获的异常