Bird
0
0

What will be the output of this code snippet?

medium📝 component behavior Q13 of 15
Node.js - Error Handling Patterns
What will be the output of this code snippet?
class NotFoundError extends Error {
  constructor(message) {
    super(message);
    this.name = 'NotFoundError';
  }
}

try {
  throw new NotFoundError('Item not found');
} catch (e) {
  console.log(e.name + ': ' + e.message);
}
AError: Item not found
BNotFoundError: Item not found
CTypeError: Item not found
Dundefined: Item not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand custom error name

    The custom error sets this.name = 'NotFoundError', so e.name is 'NotFoundError'.
  2. Step 2: Check the output format

    The console.log prints e.name + ': ' + e.message, which becomes 'NotFoundError: Item not found'.
  3. Final Answer:

    NotFoundError: Item not found -> Option B
  4. Quick Check:

    Custom error name shows in output [OK]
Quick Trick: Custom error name property appears in error output [OK]
Common Mistakes:
  • Assuming default 'Error' name instead of custom
  • Confusing error type with TypeError
  • Missing the custom name property effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes