var customError = function (){
try{
setTimeout(function(){
console.log("time out called");
throw new Error ("Error in call back");
},5000);
}catch(e){
console.log(e);
}
}
customError();
process.on('uncaughtException', function (err) {
console.log(err);
})
我读了一些博客,发现我们可以使用“uncaughtException”过程事件来处理回调中发生的错误。但另一方面,我读到你应该使用 Promise 库而不是“uncaughtException”。但“uncaughtException”事件对我来说工作得很好。我只是想知道处理回调错误是否是最佳实践?
最佳答案
你不应该真正处理“uncaughtException”,因为你不知道它来自哪里(你的应用程序或 NodeJS)。
An unhandled exception means your application - and by extension Node.js itself - is in an undefined state. Blindly resuming means anything could happen.
https://nodejs.org/api/process.html#process_event_uncaughtexception
Joyent 有一篇关于处理 NodeJS 错误的好文章,地址:https://www.joyent.com/developers/node/design/errors
关于javascript - 绑定(bind) uncaughtException 事件来处理回调 NodeJS 中发生的错误是最佳实践吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33492737/