Node.js & Libuv•Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)
Unhandled Promise Rejections & Uncaught Exceptions.
In Simple Words:
process.on('uncaughtException', (err) => ...): Synchronous uncaught error. The application is in an undefined state and must perform cleanup and exit with non-zero code.process.on('unhandledRejection', (reason, promise) => ...): Promise rejected without a.catch(). In modern Node.js, terminates the process if unhandled.
javascript
process.on('uncaughtException', (err) => {
console.error('Fatal synchronous exception:', err);
// Perform minimal synchronous logging and exit:
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
console.error('Unhandled Promise Rejection:', reason);
});