LearnFrenzy
Sign in Take a free test

Error Handling · Find the Error · Question 3

Error: a line break after throw Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function withdraw(balance, amount) {
  if (amount > balance) throw
    new Error("Insufficient balance");
  return balance - amount;
}
  • A It throws the error normally
  • B It throws undefined
  • C It returns balance - amount
  • D SyntaxError: no line break is allowed between throw and its expression (automatic semicolon insertion rules) Correct
Correct Answer: Option D

Like return, throw must have its expression on the same line. Unlike return (which would return undefined), a bare throw is a syntax error.

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