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 the purpose of the 'error' event in Node.js EventEmitter?
The 'error' event signals that an error has occurred in an EventEmitter. If not handled, it causes the program to crash. Handling this event lets you manage errors gracefully.
Click to reveal answer
beginner
How do you listen for an 'error' event on a Node.js stream?
Use the .on('error', callback) method on the stream object. The callback receives the error object to handle it properly.
Click to reveal answer
intermediate
What happens if an 'error' event is emitted but no listener is attached?
Node.js throws an uncaught 'error' event which crashes the program. Always attach an 'error' listener to avoid unexpected crashes.
Click to reveal answer
intermediate
Explain the difference between synchronous try-catch and 'error' event handling in Node.js.
Try-catch handles errors in synchronous code blocks. 'error' event handling manages asynchronous errors emitted by EventEmitters like streams or servers.
Click to reveal answer
beginner
Why is it important to handle 'error' events in Node.js applications?
Handling 'error' events prevents the app from crashing unexpectedly. It allows you to log errors, clean up resources, and provide user-friendly feedback.
Click to reveal answer
What method do you use to listen for an 'error' event on an EventEmitter?
A.on('error', callback)
B.catch('error', callback)
C.listen('error', callback)
D.handle('error', callback)
✗ Incorrect
The correct method is .on('error', callback) to listen for error events.
What happens if an 'error' event is emitted but no listener is attached?
AThe error is ignored silently
BThe program logs a warning but continues
CThe event is queued for later
DNode.js throws and crashes the program
✗ Incorrect
Without an 'error' listener, Node.js throws an uncaught error and crashes.
Which of these is NOT a correct way to handle errors in Node.js?
AIgnoring errors and continuing
BListening to 'error' events on streams
CUsing try-catch for synchronous code
DUsing promises with .catch() for async errors
✗ Incorrect
Ignoring errors can cause crashes or bugs; always handle errors properly.
In Node.js, which object commonly emits 'error' events?
AString
BArray
CEventEmitter
DNumber
✗ Incorrect
EventEmitter and its subclasses like streams emit 'error' events.
Why should you handle 'error' events on streams?
ATo prevent memory leaks
BTo avoid program crashes
CTo improve performance
DTo enable debugging mode
✗ Incorrect
Handling 'error' events prevents crashes caused by unhandled errors.
Describe how error events work in Node.js and why handling them is important.
Think about what happens if errors are not caught in event-driven code.
You got /4 concepts.
Explain the difference between synchronous error handling and error event handling in Node.js.
Consider how Node.js handles errors in different code types.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of handling error events in Node.js event emitters?
easy
A. To improve the speed of the application
B. To automatically restart the server
C. To catch and respond to problems in asynchronous code
D. To log user activity
Solution
Step 1: Understand the role of error events
Error events in Node.js are emitted when something goes wrong in asynchronous operations.
Step 2: Identify the purpose of handling errors
Handling these events allows the program to respond properly, avoiding crashes and improving stability.
Final Answer:
To catch and respond to problems in asynchronous code -> Option C
2. Which of the following is the correct way to listen for an error event on a Node.js stream named myStream?
easy
A. myStream.catch('error', (err) => { console.error(err); });
B. myStream.error((err) => { console.error(err); });
C. myStream.listen('error', (err) => { console.error(err); });
D. myStream.on('error', (err) => { console.error(err); });
Solution
Step 1: Recall the event listener syntax in Node.js
Node.js uses the on method to listen to events on event emitters like streams.
Step 2: Verify the correct method and parameters
The correct syntax is myStream.on('error', callback) where callback receives the error object.
Final Answer:
myStream.on('error', (err) => { console.error(err); }); -> Option D
Quick Check:
Use .on('error', callback) to handle errors [OK]
Hint: Use .on('error', callback) to handle errors [OK]
Common Mistakes:
Using .error() instead of .on()
Using .listen() or .catch() which don't exist
Missing the event name string 'error'
3. Consider this code snippet:
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('error', (err) => { console.log('Error caught:', err.message); });
emitter.emit('error', new Error('Oops!'));
What will be printed to the console?
medium
A. Error caught: Oops!
B. Unhandled 'error' event
C. Error: Oops!
D. No output
Solution
Step 1: Analyze the event listener setup
The code sets a listener for the 'error' event that logs the error message prefixed by 'Error caught:'.
Step 2: Understand the emitted event
The emitter emits an 'error' event with an Error object having message 'Oops!'. The listener runs and logs the message.
Final Answer:
Error caught: Oops! -> Option A
Quick Check:
Handled error event logs message = A [OK]
Hint: If error event has listener, it logs message [OK]
Common Mistakes:
Expecting unhandled error crash
Confusing error object with string output
Thinking no output occurs without console.log
4. What is wrong with this code snippet?
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.emit('error', new Error('File not found'));
medium
A. Manually emitting 'error' event is incorrect; errors should come from the system
B. The error event listener is missing, so it will crash
C. The file path should be absolute
D. createReadStream does not emit error events
Solution
Step 1: Understand error event emission
Error events on streams are emitted by the system when errors occur, not manually by user code.
Step 2: Identify the misuse of emit
Calling emit('error') manually on a stream is not standard practice and can cause unexpected behavior.
Final Answer:
Manually emitting 'error' event is incorrect; errors should come from the system -> Option A
Quick Check:
Don't manually emit 'error' on streams [OK]
Hint: Let system emit errors; don't call emit('error') yourself [OK]
Common Mistakes:
Thinking manual emit is normal
Assuming missing listener causes error here
Believing file path must be absolute always
5. You want to create a simple HTTP server in Node.js that handles errors gracefully. Which code snippet correctly handles errors on the server to avoid crashes?
hard
A. const http = require('http');
const server = http.createServer((req, res) => { res.end('Hello'); });
server.emit('error', new Error('Oops'));
server.listen(3000);