事件:'uncaughtExceptionMonitor'


【Event: 'uncaughtExceptionMonitor'

  • err <Error> 未捕获的异常。
  • origin <string> 表示异常的来源是未处理的拒绝还是同步错误。可以是 'uncaughtException''unhandledRejection'。当异常发生在基于 Promise 的异步环境中(或者 Promise 被拒绝)且 --unhandled-rejections 标志设置为 strictthrow(默认值)且拒绝未被处理时,或者当在命令行入口点的 ES 模块静态加载阶段发生拒绝时,会使用后者。

'uncaughtExceptionMonitor' 事件会在 'uncaughtException' 事件触发之前,或者通过 process.setUncaughtExceptionCaptureCallback() 安装的钩子被调用之前触发。

【The 'uncaughtExceptionMonitor' event is emitted before an 'uncaughtException' event is emitted or a hook installed via process.setUncaughtExceptionCaptureCallback() is called.】

安装 'uncaughtExceptionMonitor' 监听器不会改变 'uncaughtException' 事件触发后的行为。如果没有安装 'uncaughtException' 监听器,进程仍然会崩溃。

【Installing an 'uncaughtExceptionMonitor' listener does not change the behavior once an 'uncaughtException' event is emitted. The process will still crash if no 'uncaughtException' listener is installed.】

import process from 'node:process';

process.on('uncaughtExceptionMonitor', (err, origin) => {
  MyMonitoringTool.logSync(err, origin);
});

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
// Still crashes Node.jsconst process = require('node:process');

process.on('uncaughtExceptionMonitor', (err, origin) => {
  MyMonitoringTool.logSync(err, origin);
});

// Intentionally cause an exception, but don't catch it.
nonexistentFunc();
// Still crashes Node.js