簡體   English   中英

防止Node.js 8+退出未捕獲的異常

[英]Prevent Node.js 8+ from exiting on uncaught exceptions

免責聲明:我知道在未定義狀態下不退出應用程序的做法是多么糟糕。 我打算僅將其用於測試,因為我想知道是否完全有可能。

我認為通過處理process uncaughtException事件可以防止Node.js 8+關閉,但以下腳本在第一次throw后完成

process.on('uncaughtException', err => {
  console.log(err, 'Uncaught Exception thrown');
  // process.exit(1);
});

console.log("before throwing the first time")
throw 42;
console.log("before throwing once again")
throw 42;
console.log("after throwing a 2nd time")

我該如何預防?

您的異常處理程序正在做正確的事情。 節點退出的原因是因為您沒有其他事件要處理。 如果您還有其他事件,則該過程將繼續運行:

process.on('uncaughtException', err => {
  console.log(err, 'Uncaught Exception thrown');
  // process.exit(1);
});

setTimeout(function() { console.log('still running') }, 1000)
console.log("before throwing the first time")
throw 42;
console.log("before throwing once again")
throw 42;
console.log("after throwing a 2nd time")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM