Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEthan Goodwin
6,537 PointsHow does the ValueError as err work?
How does the code know to use the ValueError raised in the try block when the ValueError was never assigned to err? For Example:
try:
ticket_count = int(ticket_count)
# Raise a ValueError if the request is for more tickets than are avaliable
if ticket_count > tickets_remaining:
raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
except ValueError as err:
# Include the error text in the output
print("Oh no! We ran into an issue. {}. Please try again".format(err))
1 Answer
Steven Parker
231,275 PointsIt's the "except" statement that makes the assignment to "err". It looks strange because normally a variable being created and assigned goes on the left followed by an equal sign, and here it's on the right and after the word "as".
But that's what "as err
" is doing — assigning the variable "err" with the message that comes along with the exception.
Ethan Goodwin
6,537 PointsEthan Goodwin
6,537 PointsThank you, that makes more sense now!