全局的未捕获异常
REPL 使用 domain
模块来捕获该 REPL 会话的所有未捕获的异常。
在 REPL 中使用 domain
模块有这些副作用:
-
未捕获的异常仅在独立 REPL 中触发
'uncaughtException'
事件。 -
尝试使用 [
process.setExceptionCaptureCallback()
][] 会抛出ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE
错误。
process.on('uncaughtException', () => console.log('Uncaught'));
throw new Error('foobar');
// Uncaught
process.on('uncaughtException', () => console.log('Uncaught'));
// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`
//
throw new Error('foobar');
//
//
The REPL uses the domain
module to catch all uncaught exceptions for that
REPL session.
This use of the domain
module in the REPL has these side effects:
- Uncaught exceptions only emit the
'uncaughtException'
event in the standalone REPL. Adding a listener for this event in a REPL within another Node.js program throwsERR_INVALID_REPL_INPUT
. - Trying to use
process.setUncaughtExceptionCaptureCallback()
throws anERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE
error.
As standalone program:
process.on('uncaughtException', () => console.log('Uncaught'));
throw new Error('foobar');
// Uncaught
When used in another application:
process.on('uncaughtException', () => console.log('Uncaught'));
// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`
// cannot be used in the REPL
throw new Error('foobar');
// Thrown:
// Error: foobar