Bird
0
0

What is the main issue with this custom error class definition?

medium📝 Debug Q6 of 15
Node.js - Error Handling Patterns
What is the main issue with this custom error class definition?
class NetworkError extends Error { constructor(message) { this.message = message; this.name = 'NetworkError'; } }
AThe class should not extend Error for custom errors.
BThe name property should not be set manually; it is set automatically.
CThe constructor does not call super(message), so the Error class is not properly initialized.
DThe constructor should not accept any parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor implementation

    The constructor must call super(message) to initialize the base Error class properly.
  2. Step 2: Identify missing super call

    In the given code, super(message) is missing, so the error object won't have the correct message and stack trace.
  3. Step 3: Confirm name property usage

    Setting this.name is correct and necessary to identify the error type.
  4. Final Answer:

    The constructor does not call super(message), so the Error class is not properly initialized. correctly identifies the missing super call as the main issue.
  5. Quick Check:

    Constructor must call super(message) [OK]
Quick Trick: Always call super(message) in constructor [OK]
Common Mistakes:
  • Omitting super() call in constructor
  • Assuming name is set automatically
  • Not extending Error class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes