Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an uncaught exception in Node.js?
An uncaught exception is an error that occurs during program execution but is not handled by any try-catch block or error handler, causing the program to crash if not managed.
Click to reveal answer
beginner
How do you listen for uncaught exceptions in Node.js?
You use the process object's 'uncaughtException' event to listen for errors that were not caught anywhere else in the code.
Click to reveal answer
intermediate
Why should you avoid using 'uncaughtException' for normal error handling?
Because it is a last-resort handler; the application state might be unstable after an uncaught exception, so it's better to log the error and safely shut down the app.
Click to reveal answer
beginner
What is the recommended way to handle errors in asynchronous code in Node.js?
Use try-catch blocks with async/await or handle errors in callbacks/promises instead of relying on 'uncaughtException'.
Click to reveal answer
beginner
Show a simple example of handling an uncaught exception in Node.js.
process.on('uncaughtException', (err) => {
console.error('Caught exception:', err);
process.exit(1); // Exit to avoid unstable state
});
Click to reveal answer
Which Node.js event is used to catch uncaught exceptions?
A'unhandledRejection'
B'error'
C'exceptionCaught'
D'uncaughtException'
✗ Incorrect
The 'uncaughtException' event on the process object catches exceptions not handled elsewhere.
What should you do after catching an uncaught exception?
ALog the error and safely shut down the application
BIgnore the error
CLog the error and continue running normally
DRestart the Node.js process automatically
✗ Incorrect
After an uncaught exception, the app might be unstable, so it's best to log and exit safely.
Which of these is NOT a good practice for error handling in Node.js?
AUsing try-catch in async functions
BRelying only on 'uncaughtException' for all errors
CHandling promise rejections properly
DUsing callbacks with error parameters
✗ Incorrect
'uncaughtException' should be a last resort, not the main error handling method.
What happens if an uncaught exception is not handled in Node.js?
AThe program logs the error and continues
BThe program continues silently
CThe program crashes immediately
DThe program restarts automatically
✗ Incorrect
Without handling, uncaught exceptions cause the Node.js process to crash.
Which event is used to catch unhandled promise rejections in Node.js?
A'unhandledRejection'
B'rejectionHandled'
C'error'
D'uncaughtException'
✗ Incorrect
'unhandledRejection' event handles promises rejected without catch.
Explain how to handle uncaught exceptions in Node.js and why it is important.
Think about what happens if an error is not caught and how Node.js lets you listen for it.
You got /4 concepts.
Describe best practices for error handling in asynchronous Node.js code compared to using uncaughtException.
Consider how errors flow in async code and how to catch them early.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of using process.on('uncaughtException') in a Node.js application?
easy
A. To handle HTTP requests automatically
B. To log user activity in the application
C. To restart the server after every request
D. To catch errors that were not handled anywhere else in the code
Solution
Step 1: Understand the role of uncaught exceptions
Uncaught exceptions are errors that happen but are not caught by any try-catch block or error handler in the code.
Step 2: Purpose of process.on('uncaughtException')
This event listener catches those uncaught errors to prevent the app from crashing unexpectedly.
Final Answer:
To catch errors that were not handled anywhere else in the code -> Option D
Quick Check:
Uncaught exceptions = catch unhandled errors [OK]
Hint: Remember: uncaughtException catches errors missed by try-catch [OK]
Common Mistakes:
Thinking it handles normal HTTP requests
Assuming it restarts the server automatically
Confusing it with logging user actions
2. Which of the following is the correct syntax to listen for uncaught exceptions in Node.js?
easy
A. process.listen('uncaughtException', (err) => { console.error(err); });
B. process.catch('uncaughtException', (err) => { console.error(err); });
C. process.on('uncaughtException', (err) => { console.error(err); });
D. process.handle('uncaughtException', (err) => { console.error(err); });
Solution
Step 1: Identify the correct event listener method
Node.js uses process.on to listen for events like 'uncaughtException'.
Step 2: Verify the event name and callback
The event name is exactly 'uncaughtException' and the callback receives the error object.
Final Answer:
process.on('uncaughtException', (err) => { console.error(err); }); -> Option C
Quick Check:
Use process.on for events [OK]
Hint: Use process.on for event listening, not catch or listen [OK]
Common Mistakes:
Using process.catch instead of process.on
Using process.listen or process.handle which don't exist
Misspelling the event name
3. Consider the following Node.js code snippet:
process.on('uncaughtException', (err) => {
console.log('Caught:', err.message);
});
throw new Error('Oops!');
What will be the output when this code runs?
medium
A. The program crashes without any output
B. Caught: Oops!
C. Error: Oops!
D. No output, the error is ignored
Solution
Step 1: Understand the uncaughtException handler
The handler logs the error message prefixed with 'Caught:'.
Step 2: The thrown error triggers the handler
The thrown error 'Oops!' is caught by the listener and logged as 'Caught: Oops!'.
But the program crashes after the error is logged. What is the best fix?
medium
A. Add process.exit(1); inside the handler after logging
B. Remove the throw statement inside setTimeout
C. Wrap the throw inside a try-catch block
D. Use process.on('error') instead of uncaughtException
Solution
Step 1: Understand uncaughtException behavior
After catching, the app is in an unstable state and should exit safely.
Step 2: Add process.exit(1) to stop the app
Calling process.exit(1) after logging ensures the app stops cleanly.
Final Answer:
Add process.exit(1); inside the handler after logging -> Option A
Quick Check:
Exit after uncaughtException to avoid unstable state [OK]
Hint: Exit process after uncaughtException to avoid crashes [OK]
Common Mistakes:
Ignoring the need to exit after catching
Trying to catch error inside setTimeout instead
Using wrong event name 'error' instead of 'uncaughtException'
5. You want to log uncaught exceptions and then restart your Node.js server automatically. Which approach correctly combines handling uncaught exceptions and restarting the app?
hard
A. Use process.on('uncaughtException') to log error, then call process.exit(1), and use a separate script or tool to restart the server
B. Inside uncaughtException handler, just call server.listen() again to restart
C. Wrap the entire app code in a try-catch block to restart on error
D. Use process.on('exit') to catch errors and restart the server
Solution
Step 1: Handle uncaught exceptions by logging and exiting
Inside the handler, log the error and call process.exit(1) to stop the app safely.
Step 2: Use an external tool or script to restart the server
Node.js itself does not restart automatically; tools like PM2 or systemd can restart on exit.
Final Answer:
Use process.on('uncaughtException') to log error, then call process.exit(1), and use a separate script or tool to restart the server -> Option A