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.
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 :(
Chris FreemanTreehouse Moderator 68,029 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.
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 Yadav1,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?