Node.js应用程序因setInterval中的无效异常而终止。我尝试通过process.on('uncaughtException',..)和域方法(参见下面的代码)修复它。虽然处理了异常,但应用程序仍然被终止。如何防止node.js应用程序终止在setInterval中未处理的异常?
function f() {
throw Error('have error')
}
process.on('uncaughtException', function(err){
console.log("process.on uncaughtException")
})
var d = require('domain').create();
d.on('error',function(err){
console.log("domain.on error")
})
d.run(function(){
setInterval(f, 1000)
})
// program terminated and output is: domain.on error
http://nodejs.org/api/domain.html#domain_warning_don_t_ignore_errors – jgillich
@ jgillich的链接非常重要。域不是为了避免崩溃,它们意味着在错误发生之后清理并最终关闭。如果一个人死亡,你通常会使用一个进程监视器来启动一个新进程。 – loganfsmyth