全局的未捕获异常


REPL使用 domain 模块来捕获会话期间的所有未捕获异常。

在REPL中使用 domain 模块有如下副作用:

作为独立程序:

process.on('uncaughtException', () => console.log('未捕获的异常'));

throw new Error('foobar');
// 打印:未捕获的异常

当在其他应用程序中使用时:

process.on('uncaughtException', () => console.log('未捕获的异常'));
// 抛出 TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException` cannot be used in the REPL

throw new Error('foobar');
// 抛出:
// 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:

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