即使捕获未捕获的exception,Node.js也会在TypeError上崩溃
我注意到Node.js在types错误上崩溃,即使你捕获未捕获的exception,下面是一些示例代码:
var http = require('http'); process.on('uncaughtException', function (err) { console.log(err.stack); }); var test = []; // This causes the error because test[0] is undefined console.log(test[0].toLowerCase()); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, "127.0.0.1");
有什么办法来捕捉错误,并保持node.js运行。
更新:我应该提到uncaughtException处理程序在这种情况下触发,但它仍然崩溃后。
exception本身并没有closures你的程序,节点正在停止,因为事件队列中什么也没有。
exception仍然停止执行当前的函数/上下文,所以它永远不会到达你的http.createServer
。 如果您将错误移动到http命令之外,它将按预期工作。 🙂