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 trial

Python Python Basics All Together Now Handle Exceptions

Ali AlRabeah
Ali AlRabeah
909 Points

Is err equal to whatever ValueError we raised beforehand?

In these lines of codes, is err in line 5 equal to raise in line 4? Is this always the case? If so, what happens when you have multiple "raise ValueError"'s before and err?

try:
    tickets_selected = int(tickets_selected)
    if tickets_selected > tickets_remaining:
        raise ValueError("We don't have that many tickets")
except ValueError as err:
    print("That's an invalid answer. {}. Please try again.".format(err))

What does "except ValueError as err" actually mean and what does "err" represent?

1 Answer

From my understanding (and I hope to be corrected if I'm wrong), "except ValueError as err:" is basically saying: "Take this ValueError we just raised, and put it into a new variable we'll call "err", so that we can do something with it." In this case, print it out so it's visible to the user, without crashing the program.