Bird
Raised Fist0
Node.jsframework~20 mins

Handling uncaught exceptions in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Node.js Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What happens when an uncaught exception occurs in Node.js?

Consider a Node.js application without any error handling for exceptions. What is the typical behavior when an uncaught exception occurs?

ANode.js automatically retries the failed operation causing the exception.
BThe exception is silently ignored and the process continues running.
CThe Node.js process immediately exits with a non-zero exit code.
DThe exception is logged but the process keeps running without interruption.
Attempts:
2 left
💡 Hint

Think about what happens if an error is not caught anywhere in the code.

component_behavior
intermediate
2:00remaining
What is the output of this Node.js code handling uncaught exceptions?

Analyze the following Node.js code snippet and select the correct console output.

Node.js
process.on('uncaughtException', (err) => {
  console.log('Caught exception:', err.message);
});

throw new Error('Test error');
console.log('This line will run');
ACaught exception: Test error
B
Caught exception: Test error
This line will run
C
This line will run
Caught exception: Test error
DNo output, process crashes silently
Attempts:
2 left
💡 Hint

Consider what happens after an uncaught exception is thrown and caught by the handler.

🔧 Debug
advanced
2:00remaining
Identify the error in this uncaught exception handler code

Find the mistake in this Node.js code that tries to handle uncaught exceptions.

Node.js
process.on('uncaughtException', (error) => {
  console.error('Error caught:', error);
  process.exit(1);
});

setTimeout(() => {
  throw new Error('Failure');
}, 100);

console.log('App started');
AThe error parameter should be named err, otherwise it won't catch the exception.
BThe handler calls process.exit(1) which stops the process immediately, so asynchronous cleanup code won't run.
CThe setTimeout callback should be synchronous to catch the error properly.
DThe console.log('App started') will never run because the exception is thrown.
Attempts:
2 left
💡 Hint

Think about what happens when process.exit is called inside the handler.

📝 Syntax
advanced
2:00remaining
Which option correctly attaches an uncaught exception handler in Node.js?

Select the code snippet that correctly listens for uncaught exceptions.

Aprocess.on('uncaughtException', (err) => { console.log(err.message); });
Bprocess.catch('uncaughtException', (err) => { console.log(err); });
Cprocess.addListener('uncaughtException', function errorHandler() { console.log('Error'); });
Dprocess.handle('uncaughtException', (error) => { console.error(error); });
Attempts:
2 left
💡 Hint

Recall the correct method name to listen to events on the process object.

lifecycle
expert
3:00remaining
What is the best practice for handling uncaught exceptions in a production Node.js app?

Choose the most appropriate approach to handle uncaught exceptions in a production environment.

ACatch all exceptions globally and retry the failed operation automatically.
BIgnore the exception and let the process continue running to avoid downtime.
CUse <code>process.exit(0)</code> inside the handler to restart the process silently.
DLog the error, perform any synchronous cleanup, then gracefully shut down the process.
Attempts:
2 left
💡 Hint

Think about stability and data integrity when an unexpected error occurs.

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

  1. 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.
  2. Step 2: Purpose of process.on('uncaughtException')

    This event listener catches those uncaught errors to prevent the app from crashing unexpectedly.
  3. Final Answer:

    To catch errors that were not handled anywhere else in the code -> Option D
  4. 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

  1. Step 1: Identify the correct event listener method

    Node.js uses process.on to listen for events like 'uncaughtException'.
  2. Step 2: Verify the event name and callback

    The event name is exactly 'uncaughtException' and the callback receives the error object.
  3. Final Answer:

    process.on('uncaughtException', (err) => { console.error(err); }); -> Option C
  4. 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

  1. Step 1: Understand the uncaughtException handler

    The handler logs the error message prefixed with 'Caught:'.
  2. Step 2: The thrown error triggers the handler

    The thrown error 'Oops!' is caught by the listener and logged as 'Caught: Oops!'.
  3. Final Answer:

    Caught: Oops! -> Option B
  4. Quick Check:

    Uncaught error triggers handler output [OK]
Hint: Thrown error triggers uncaughtException handler output [OK]
Common Mistakes:
  • Expecting the program to crash immediately
  • Thinking the error message prints with 'Error:' prefix
  • Assuming no output because error is ignored
4. You wrote this code to catch uncaught exceptions:
process.on('uncaughtException', (error) => {
  console.log('Error:', error.message);
});

setTimeout(() => {
  throw new Error('Fail');
}, 1000);

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

  1. Step 1: Understand uncaughtException behavior

    After catching, the app is in an unstable state and should exit safely.
  2. Step 2: Add process.exit(1) to stop the app

    Calling process.exit(1) after logging ensures the app stops cleanly.
  3. Final Answer:

    Add process.exit(1); inside the handler after logging -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. Quick Check:

    Log + exit + external restart tool = correct approach [OK]
Hint: Exit on error, restart externally (PM2/systemd) for stability [OK]
Common Mistakes:
  • Trying to restart server inside uncaughtException handler
  • Using process.on('exit') to catch errors (wrong event)
  • Wrapping entire app in try-catch which misses async errors