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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Olivia Earenfight](https://secure.gravatar.com/avatar/956535bbc5cb2d5fe58a62ded285314c?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Olivia Earenfight
3,509 PointsAs err
Hi,
I am confused with using "as" to reference a raised exception. From the 3:00 minute mark to the 4:00 minute mark in this video, I got a bit lost. First, the messaging for the raised exception disappears. I don't understand why the messaging does not appear, but I'm also confused as to how using the keyword "as" prints the messaging from the raised exception. For example:
except ValueError as err: print("Oh no! That's not a valid value. Try again..") print("({})".format(err))
I don't understand how adding in a variable refers to the text in the raised exception.
Thanks!
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsIt's part of how exceptions are handled. I suppose you might say "it just works because it was made to", but then you can say that about most things a programming language does.
![fady mohamed](https://secure.gravatar.com/avatar/732f7ee17dd278a1c2a24e63a3ac9367?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
fady mohamed
5,972 PointsAhh right thanks Steven. So "as" is only used in exceptions? I assumed "as" might be used in other instances too. Or does it work differently depending on where it's used.
3 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe variable gets filled with an exception object (which contains the text) as part of the "except" handling.
I think of "as" being a type of assignment that occurs only after the exception happens.
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsIt is the "aliasing" operator, and not just for exceptions. Another usage example:
with open(file) as f:
![Hanneke Lu](https://secure.gravatar.com/avatar/9dd776056dcfea5d4d00f9a923c0117a?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Hanneke Lu
560 PointsI am having a hard time getting the full understanding of the 'ValueError as err', and suspected it also had to do with not understanding the working of 'as' in Python. The following helped me understand a bit better:
https://www.quora.com/When-would-one-use-as-in-Python
But, I still don't get it completely. Why is the message from the raise ValueError ("More than one person is required to split the check") not displayed? (when Craig says: 'now we lost our messaging')
Why is it displayed as soon as Craig changes the 'except ValueError' into 'except ValueError as err'?
I hope you can help! I'm a beginner so if it's possible to use layman's terms, please do so (I'm not familiar with terms like 'aliasing' yet. I did try to figure stuff out on the internet for about an hour, before asking this question. But figured I really need your help!)
![Bryan Bee](https://secure.gravatar.com/avatar/4018aa2fdb6a68f8c4710042e2ba69a8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Bryan Bee
477 Pointshow does it know that the "err" I want it to write ties back to the "needs more than 1 person" that we wrote in the "if...raise"? and why does the "if...raise" get ignored just because i didn't write the err? I'm having trouble seeing the step-by-step that the computer would take
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe argument you pass in when you raise the error gets stored in the exception object that will get put into the "as" variable.
And "step-by-step" is a good idea for understanding the process. Try looking at one line at a time while assuming the variables have a specific value and note down what each line of code will do.
fady mohamed
5,972 Pointsfady mohamed
5,972 PointsI'm also confused regarding this. Why does err store the text from the raised exception in split_check? I experimented by adding an elif to split_check as below, and found that it really does store the text from whichever if statement was true in split_check. my question is how does the "as" work in storing the text? I don't believe it was explained in the video. unless there is no explanation and it just works because it was made to and that is its only purpose?