Bird
0
0

Identify the issue in this async error handling code:

medium📝 Debug Q6 of 15
Node.js - Error Handling Patterns
Identify the issue in this async error handling code:
async function fetchUser() {
  try {
    const user = await getUserData();
  } catch {
    console.error('Failed:', err);
  }
}
AThe catch block does not declare the error parameter 'err'
BThe await keyword is missing before getUserData()
CThe function fetchUser() should not be async
Dconsole.error cannot be used inside catch blocks
Step-by-Step Solution
Solution:
  1. Step 1: Review catch syntax

    In JavaScript, catch blocks must declare an error parameter to access the error object.
  2. Step 2: Analyze the code

    The catch block uses 'err' but does not declare it: catch { ... } instead of catch (err) { ... }.
  3. Step 3: Consequence

    This causes a ReferenceError because 'err' is undefined inside catch.
  4. Final Answer:

    The catch block does not declare the error parameter 'err' -> Option A
  5. Quick Check:

    Is the error parameter declared in catch? [OK]
Quick Trick: Always declare error parameter in catch (e.g., catch (err)) [OK]
Common Mistakes:
  • Omitting error parameter in catch block
  • Forgetting to use await before async calls
  • Misusing console.error inside catch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes