LearnFrenzy
Sign in Take a free test

Error Handling · Find the Error · Question 9

Error: re-throwing the wrong name Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function charge(amount) {
  try {
    if (amount <= 0) throw new RangeError("amount must be positive");
    return "charged";
  } catch (err) {
    console.log("logging failure…");
    throw error;
  }
}
charge(0);
  • A RangeError: amount must be positive
  • B ReferenceError: error is not defined – the catch variable is err, so the re-throw itself fails and hides the RangeError Correct
  • C It returns "charged"
  • D SyntaxError
Correct Answer: Option B

Write throw err;. The ReferenceError replaces the real problem, making the bug harder to diagnose.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links