0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
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.