Bird
0
0

Identify the error in this Node.js code snippet:

medium📝 Debug Q14 of 15
Node.js - Error Handling Patterns
Identify the error in this Node.js code snippet:
try {
  console.log('Start');
  throw 'Error happened';
} catch {
  console.log('Caught an error');
}
AThrowing a string instead of an Error object is invalid.
BMissing error parameter in catch block parentheses.
CThe try block must not contain console.log statements.
DCatch block must be followed by finally block.
Step-by-Step Solution
Solution:
  1. Step 1: Check catch block syntax

    In Node.js, catch must have parentheses with an error parameter, e.g., catch (error).
  2. Step 2: Validate other parts

    Throwing a string is allowed, console.log is fine, and finally is optional.
  3. Final Answer:

    Missing error parameter in catch block parentheses. -> Option B
  4. Quick Check:

    Catch needs error param = B [OK]
Quick Trick: Catch always needs (error) parameter in Node.js [OK]
Common Mistakes:
  • Thinking catch can omit error parameter
  • Believing throw must use Error object only
  • Assuming finally block is mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes