Performance: Handling uncaught exceptions
This affects the server's uptime and responsiveness by preventing crashes from unexpected errors.
Jump into concepts and practice - no test required
process.on('uncaughtException', (err) => { console.error('Fatal error:', err); process.exit(1); // Exit to avoid unstable state });
process.on('uncaughtException', (err) => { console.log('Error:', err); // No process exit or recovery });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Logging error without exit | N/A | N/A | N/A | [X] Bad |
| Logging error with process exit | N/A | N/A | N/A | [OK] Good |
| No cleanup on error | N/A | N/A | N/A | [X] Bad |
| Graceful shutdown before exit | N/A | N/A | N/A | [OK] Good |
process.on('uncaughtException') in a Node.js application?process.on('uncaughtException')process.on to listen for events like 'uncaughtException'.process.on('uncaughtException', (err) => {
console.log('Caught:', err.message);
});
throw new Error('Oops!');process.on('uncaughtException', (error) => {
console.log('Error:', error.message);
});
setTimeout(() => {
throw new Error('Fail');
}, 1000);process.exit(1) after logging ensures the app stops cleanly.process.exit(1) to stop the app safely.