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

"as err:" in "except ValueError as err:"

I'm assuming and my googling skills are apparently not as good as I thought but:

I don't know if I've missed it or not understanding, but if "except ValueError:" works without the "as err". From reading, watching, and playing with it, I'm assuming it just displays an error message . I would like to know when its used, what its purpose is, and if its only for displaying an error messages.

1 Answer

Adding the as err includes the actual message. Check out this link to an explanation - does that help clear it up.

Also, here's some test code:

try:
    z = int('text')
except ValueError:
    print('got a value error on text')

try:
    x = int('text')
except ValueError as err:
    print('The actual error text is --> ', err, ' <--')

#  output is:
got a value error on text
The actual error text is -->  invalid literal for int() with base 10: 'text'  <-- 

Is that what you were asking?

Exactly the answer I was looking for!