Handling Uncaught Exceptions and Rejections Correcty
Updated May 4, 2026
Short answer
Log the error, perform a graceful cleanup, and always exit the process. Use a process manager (PM2/K8s) to restart the instance.
Deep explanation
When an uncaughtException occurs, the application is in an 'undefined state'. The memory might be corrupted, or database connections might be hung. Node.js documentation explicitly recommends not trying to resume execution. The correct pattern is to log the stack trace to an external service (Sentry/Datadog), stop the server from accepting new requests, wait a few seconds for current ones to finish, and process.exit(1). This follows the 'Fail Fast' philosophy.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro