Interview frequency: …
Short answer
uncaughtException occurs on a synchronous error not caught by try/catch and the process should be terminated then, while unhandledRejection occurs when a promise fails with no catch handler.
Explanation
When an unhandled synchronous exception is thrown, the Node.js application's state becomes unstable and unguaranteed; the officially recommended action is to log the error, gracefully close the current connections, then terminate the process (process.exit(1)) and let the process manager restart it.
Code example
process.on("uncaughtException", (err) => {
console.error("Critical error:", err);
process.exit(1);
});Common mistakes
- Trying to continue running the server after an uncaughtException, which leads to data corruption or a memory leak.