Bird
0
0

Which of the following correctly defines a custom error class InputError that extends the built-in Error class in Node.js?

easy📝 Syntax Q3 of 15
Node.js - Error Handling Patterns
Which of the following correctly defines a custom error class InputError that extends the built-in Error class in Node.js?
Aclass InputError extends Error { constructor(message) { super(message); this.name = 'InputError'; } }
Bclass InputError { constructor(message) { this.message = message; this.name = 'InputError'; } }
Cfunction InputError(message) { this.message = message; this.name = 'InputError'; }
Dclass InputError extends Error { constructor() { this.name = 'InputError'; } }
Step-by-Step Solution
Solution:
  1. Step 1: Extend the built-in Error class

    This ensures the custom error inherits standard error properties and behavior.
  2. Step 2: Call super(message) in the constructor

    This initializes the message property correctly on the Error object.
  3. Step 3: Set the name property explicitly

    Setting this.name helps identify the error type when caught.
  4. Final Answer:

    class InputError extends Error { constructor(message) { super(message); this.name = 'InputError'; } } correctly implements all these steps.
  5. Quick Check:

    Extends Error + super(message) + name set [OK]
Quick Trick: Always extend Error and call super(message) [OK]
Common Mistakes:
  • Not extending Error class
  • Forgetting to call super(message)
  • Not setting the name property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes