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 Functions and Looping Raising Exceptions

Hi there Can someone please help explain what Craig is trying to explain at 3:07 about the (as and err)?

except ValueError as err:

I dont not understand :(

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Daniel Liew Yew Onn, good question. An except statement handles specific errors that might arise during execution of the code following the try. The raised exception is an instance that has attributes.

According to the docs:

The except clause may specify a variable after the exception name. The variable is bound to an exception instance with the arguments stored in instance.args.

So the as err says "allow me to refer to the raised exception by the variable name err". Using the err reference, the exception attributes may be accessed. If the err instance has a __str__ method defined, then a print(err) will call that method and a string will be output.

Post back if you need more help. Good Luck!!

Alok Yadav
Alok Yadav
1,023 Points

Where is the err variable defined? If there are 2 types of errors in the code, how will it know which one the err is referring to?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

While there are two possible places an error could be raised, but only one can be first. Whichever one is raised first is the error assigned to err

Szymon Dabrowski
Szymon Dabrowski
2,207 Points

Chris Freeman, thank you kindly for this explanation. You have helped me out a ton!