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

Olivia Earenfight
Olivia Earenfight
3,509 Points

As 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!

I'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?

def split_check(total, number_of_people):
    if number_of_people <= 1:
        raise ValueError("More than 1 person is required to split check")
    elif number_of_people >=5:
        raise ValueError("Are you sure it's that many people?")
    return math.ceil(total / number_of_people)
Steven Parker
Steven Parker
231,268 Points

It'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.

Ahh 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
Steven Parker
231,268 Points

The 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
Steven Parker
231,268 Points

It is the "aliasing" operator, and not just for exceptions. Another usage example:

with open(file) as f:
Hanneke Lu
Hanneke Lu
560 Points

I 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
Bryan Bee
477 Points

how 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
Steven Parker
231,268 Points

The 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.