node.js - 抛出异常会杀死 Node js中的线程吗?

标签 node.js

据我所知,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/

相关文章:

javascript - 错误 'getaddrinfo EAI_AGAIN' 的原因是什么?

node.js - 如何让 Docker 容器和 Node 应用程序在 EC2 上运行?

node.js - 有时环回当前上下文为空

javascript - ES6 "Import()"问题 "MODULE_NOT_FOUND"[Webpack - NodeJS]

node.js - 如何在nodejs邮件程序emailJS中设置回复?

javascript - Socket.io 和 Async 不返回值

node.js - 使用 gulp-vinyl-zip 创建 zip 文件时出现 TypeError

javascript - Promise 只是让异步代码同步吗?为什么不直接使用同步代码呢?

javascript - 一个我看不懂的nodejs回调执行顺序问题的例子

javascript - 明确存在的功能被识别为未定义