据我所知,node js 是单线程的,所以如果我写
throw new Error()
在方法的某处,它应该杀死当前线程,所以会关闭整个进程?
最佳答案
阅读 NodeJs documentation about exceptions帮助您很容易地理解发生了什么。
关于 Error Propagation and 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.
并回答您的问题:
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.
所以基本上任何未处理的异常都会杀死整个 Node 进程。
这就是 PM2 之类的工具所在的位置启动,每当发生这种情况时重新启动您的 Node 应用程序。
虽然this来自 2012 年,我认为这是一篇不错的文章,并且非常容易理解您应该如何处理未捕获的异常
关于node.js - 抛出异常会杀死 Node js中的线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35553485/