Spring Fresh Sale! - Up To 67% OFF BDIX Hosting + Free Domain
Node.js

How to Handle Uncaught Exceptions and Unhandled Promise Rejections in Node.js

An uncaught exception or unhandled promise rejection means an error escaped all your error handling — and by default it can crash your Node.js process. Handling these correctly is about failing safely, logging what happened, and restarting cleanly rather than pretending nothing went wrong.

The two escape hatches

  • uncaughtException — a synchronous error nobody caught.
  • unhandledRejection — a rejected Promise with no .catch() or try/catch around an await.

The right way to handle them

You can listen for these as a last resort, but the key insight is: after an uncaught exception, your app is in an unknown state. The safe pattern is to log the error, then exit and let your process manager restart a fresh instance:

process.on('uncaughtException', (err) => { logger.fatal(err); process.exit(1); });

A process manager like PM2 then restarts it immediately, so users barely notice.

Prevent them in the first place

Why not just keep running?

Swallowing an uncaught exception and continuing risks corrupted state, leaked resources and confusing bugs. Logging then restarting is the industry-standard "let it crash, then recover" approach — cleaner and safer.

Frequently asked questions

Should I restart after an uncaught exception?

Yes. After one, the app's state is unreliable, so the safe move is to log and exit, letting your process manager start a fresh instance. Continuing risks worse, harder-to-trace problems.

Why is my app crashing on an unhandled rejection?

Modern Node treats unhandled promise rejections as fatal by default. The fix is to handle the rejection where it occurs, not to suppress the behaviour — track down the Promise missing a catch.

Can a process manager handle restarts for me?

Yes — PM2 or a systemd service restarts a crashed app automatically. Combined with logging, this gives you resilient recovery with minimal downtime. See running Node as a systemd service.

Was this article helpful?