'uncaughtException' 事件


当未捕获的 JavaScript 异常一直冒泡回到事件循环时,会触发 'uncaughtException' 事件。 默认情况下,Node.js 通过将堆栈跟踪打印到 stderr 并使用退出码 1 来处理此类异常,从而覆盖任何先前设置的 process.exitCode。 为 'uncaughtException' 事件添加处理程序会覆盖此默认行为。 或者,更改 'uncaughtException' 处理程序中的 process.exitCode,这将导致进程退出并提供退出码。 否则,在存在这样的处理程序的情况下,进程将以 0 退出。

调用监听器函数时,将 Error 对象作为唯一参数传入。

process.on('uncaughtException', (err) => {
  fs.writeSync(1, `捕获的异常:${err}\n`);
});

setTimeout(() => {
  console.log('这里仍然会运行');
}, 500);

// 故意引起异常,但不要捕获它。
nonexistentFunc();
console.log('这里不会运行');

The 'uncaughtException' event is emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. By default, Node.js handles such exceptions by printing the stack trace to stderr and exiting with code 1, overriding any previously set process.exitCode. Adding a handler for the 'uncaughtException' event overrides this default behavior. Alternatively, change the process.exitCode in the 'uncaughtException' handler which will result in the process exiting with the provided exit code. Otherwise, in the presence of such handler the process will exit with 0.

The listener function is called with the Error object passed as the only argument.

process.on('uncaughtException', (err) => {
  fs.writeSync(1, `Caught exception: ${err}\n`);
});

setTimeout(() => {
  console.log('This will still run.');
}, 500);

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
console.log('This will not run.');