Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,515 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,022 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 67,515 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,160 Points

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