Bird
0
0

You wrote this code to catch unhandled promise rejections:

medium📝 Debug Q14 of 15
Node.js - Error Handling Patterns
You wrote this code to catch unhandled promise rejections:
process.on('unhandledRejection', (error) => {
  console.log('Caught:', error);
});

Promise.reject('Oops!');

But the console shows Caught: Oops! instead of an error message. What is the issue?
AThe handler function must be async to catch rejections
BThe event name should be 'unhandledRejections' (plural)
CYou must use try-catch instead of process.on for promises
DThe rejection reason is a string, not an Error object, so error.message is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Check the rejection reason type

    The promise rejects with a string 'Oops!', not an Error object.
  2. Step 2: Understand how the handler logs the error

    The handler logs the whole error variable, which is the string 'Oops!', so it prints exactly that.
  3. Final Answer:

    The rejection reason is a string, not an Error object, so error.message is undefined -> Option D
  4. Quick Check:

    Rejection reason type affects error.message presence [OK]
Quick Trick: Rejection reason can be any type; strings have no .message [OK]
Common Mistakes:
  • Using wrong event name 'unhandledRejections'
  • Thinking async handler is required
  • Believing try-catch catches unhandled rejections

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes