LearnFrenzy
Sign in Take a free test

Exception Handling · Find the Error · Question 5

Bug: the specific message never appears Answer, explanation & discussion

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

A non-numeric quantity should print "Quantity must be a number". Which statement correctly identifies the bug?

main.py
qty_text = "ten"
try:
    qty = int(qty_text)
except Exception:
    print("Something went wrong")
except ValueError:
    print("Quantity must be a number")
  • A int("ten") raises TypeError
  • B Only one except clause is allowed
  • C except Exception comes first and also matches ValueError, so the specific handler is unreachable; put except ValueError first Correct
  • D The handlers run in reverse order
Correct Answer: Option C

Order handlers from specific to general.

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