'uncaughtException' 事件


  • err <Error> 未捕获的异常。
  • origin <string> 表明异常的来源(来自未处理的拒绝、或来自同步的错误)。 可以是 'uncaughtException''unhandledRejection'。 后者仅用于 --unhandled-rejections 标志被设置为 strictthrow 且有未处理的拒绝。

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

process.on('uncaughtException', (err, origin) => {
  fs.writeSync(
    process.stderr.fd,
    `捕获的异常: ${err}\n` +
    `异常的来源: ${origin}`
  );
});

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

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

通过添加 'uncaughtExceptionMonitor' 事件监听器,可以监视 'uncaughtException' 事件但不覆盖默认的退出进程的行为。

  • err <Error> The uncaught exception.
  • origin <string> Indicates if the exception originates from an unhandled rejection or from an synchronous error. Can either be 'uncaughtException' or 'unhandledRejection'. The latter is only used in conjunction with the --unhandled-rejections flag set to strict or throw and an unhandled rejection.

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.

process.on('uncaughtException', (err, origin) => {
  fs.writeSync(
    process.stderr.fd,
    `Caught exception: ${err}\n` +
    `Exception origin: ${origin}`
  );
});

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

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

It is possible to monitor 'uncaughtException' events without overriding the default behavior to exit the process by installing a 'uncaughtExceptionMonitor' listener.