0
0
Node.jsframework~20 mins

Error events and handling in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an 'error' event is emitted without a listener?
Consider this Node.js code snippet that emits an 'error' event on an EventEmitter without any listener attached. What will happen when this code runs?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.emit('error', new Error('Something went wrong'));
AThe error event is queued until a listener is added.
BThe error event is ignored silently.
CThe error is logged to the console but the program continues.
DThe program throws an uncaught error and crashes.
Attempts:
2 left
💡 Hint
Think about Node.js default behavior when an 'error' event has no listeners.
state_output
intermediate
2:00remaining
What is the output after handling an error event?
Given this code, what will be printed to the console?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('error', (err) => {
  console.log('Caught error:', err.message);
});
emitter.emit('error', new Error('Oops!'));
AUncaught Error: Oops!
BCaught error: Oops!
CNothing is printed.
DCaught error: undefined
Attempts:
2 left
💡 Hint
Check how the error message is accessed inside the listener.
📝 Syntax
advanced
2:00remaining
Which option correctly attaches an error event listener?
Select the code snippet that correctly attaches an 'error' event listener to an EventEmitter instance named 'emitter'.
Aemitter.addListener('error', err => console.log(err.message));
Bemitter.on('error', function(err) { console.log(err); });
Cemitter.error(function(err) { console.log(err); });
Demitter.listen('error', (err) => console.log(err));
Attempts:
2 left
💡 Hint
Check the correct method names for attaching event listeners in Node.js EventEmitter.
🔧 Debug
advanced
2:00remaining
Why does this error handler not catch the error?
Look at this code snippet. Why does the error handler not catch the emitted error?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.emit('error', new Error('Fail'));
emitter.on('error', (err) => {
  console.log('Error caught:', err.message);
});
AThe error event was emitted before the listener was attached.
BThe listener function has a syntax error.
CThe error object is not passed correctly to the listener.
DThe EventEmitter instance does not support error events.
Attempts:
2 left
💡 Hint
Consider the order of event emission and listener attachment.
🧠 Conceptual
expert
3:00remaining
What is the best practice to avoid crashing on error events in Node.js?
Which approach best prevents the Node.js process from crashing due to unhandled 'error' events on EventEmitters?
AUse process.on('uncaughtException') to handle all errors globally.
BWrap every emit call in try-catch blocks.
CAlways attach an 'error' event listener to every EventEmitter instance.
DIgnore error events and let the process crash to fix bugs quickly.
Attempts:
2 left
💡 Hint
Think about how Node.js expects error events to be handled on EventEmitters.