Bird
0
0

What will be the output of this code?

medium📝 component behavior Q4 of 15
Node.js - Error Handling Patterns
What will be the output of this code?
class MyError extends Error { constructor(msg) { super(msg); this.name = 'MyError'; } } try { throw new MyError('Oops!'); } catch (e) { console.log(e.name + ': ' + e.message); }
Aundefined: Oops!
BError: Oops!
COops!: MyError
DMyError: Oops!
Step-by-Step Solution
Solution:
  1. Step 1: Analyze error throwing

    The code throws an instance of MyError with message 'Oops!'.
  2. Step 2: Check catch block output

    The catch block logs the error's name and message. The name is set to 'MyError' in constructor, message is 'Oops!'.
  3. Final Answer:

    MyError: Oops! -> Option D
  4. Quick Check:

    Error name and message output = MyError: Oops! [OK]
Quick Trick: Custom error name property controls error type shown [OK]
Common Mistakes:
  • Expecting default 'Error' name instead of custom
  • Swapping message and name in output
  • Assuming undefined name property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes