0
0
Node.jsframework~10 mins

Handling uncaught exceptions in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling uncaught exceptions
Start Node.js Process
Run Code
Exception Occurs?
NoContinue Running
Yes
Is Exception Caught?
YesHandle Exception
No
Trigger 'uncaughtException' Event
Run uncaughtException Handler
Decide to Exit or Continue
Process Ends or Continues
Node.js runs code and if an error happens without a catch, it triggers the 'uncaughtException' event where you can handle it before deciding to exit or continue.
Execution Sample
Node.js
process.on('uncaughtException', (err) => {
  console.log('Caught:', err.message);
});

throw new Error('Oops!');
This code sets a handler for uncaught exceptions and then throws an error that triggers it.
Execution Table
StepActionEvaluationResult
1Set 'uncaughtException' handlerHandler registeredReady to catch errors
2Throw new Error('Oops!')Error thrown, no try-catchUncaught exception triggered
3'uncaughtException' event firesHandler runs with error objectLogs 'Caught: Oops!'
4No process exit calledProcess continues runningProgram does not crash here
💡 Execution stops if process.exit() is called inside handler; otherwise, process continues.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
err.messageundefinedError object with message 'Oops!''Oops!''Oops!'
Key Moments - 3 Insights
Why doesn't the program crash immediately after the error is thrown?
Because the 'uncaughtException' handler catches the error at step 3, preventing the default crash behavior.
Can the program continue running safely after an uncaught exception?
It can continue if you don't call process.exit() in the handler, but it's risky because the program state may be unstable (see step 4).
What happens if there is no 'uncaughtException' handler?
Node.js will print the error and exit the process immediately after the uncaught exception occurs (step 2 triggers crash).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is logged when the 'uncaughtException' event fires?
A'Unhandled Exception'
B'Caught: Oops!'
C'Error: Oops!'
DNothing is logged
💡 Hint
Check row 3 in the execution_table where the handler logs the error message.
At which step does the error become an uncaught exception triggering the event?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the execution_table row where the error is thrown without a try-catch.
If process.exit() is called inside the handler, what changes in the execution flow?
AProgram continues running normally
BProgram logs error but does not exit
CProgram exits immediately after handling error
DHandler is never called
💡 Hint
Refer to the exit_note explaining process exit behavior after handler runs.
Concept Snapshot
Handling uncaught exceptions in Node.js:
- Use process.on('uncaughtException', handler) to catch errors not caught by try-catch.
- The handler receives the error object.
- You can log or clean up inside the handler.
- Calling process.exit() stops the program; otherwise, it continues but may be unstable.
- Without a handler, Node.js crashes on uncaught exceptions.
Full Transcript
In Node.js, when your code throws an error that is not caught by any try-catch block, it triggers an 'uncaughtException' event. You can listen for this event using process.on('uncaughtException', handler). The handler receives the error object and can log it or perform cleanup. If you do not call process.exit() inside the handler, the program continues running, but this can be risky because the program state might be corrupted. If no handler is set, Node.js will print the error and exit immediately. This flow helps you catch unexpected errors globally and decide how to handle them safely.