Question 5
Back to Exercise
A non-numeric quantity should print "Quantity must be a number". Which statement correctly identifies the bug?
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!