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

ValueError as err: ???

Hello everybody,

Okay so i totally understand the video my only questions have to do specifically with "as err:" am i correct to assume 'as' is similar to ' for ' , 'in:' , 'def' ...meaning it is what it is and thats how it is? and the use of it is self explanitory?

err.. is this a python built in function or variable or type.....? or is it something that can be changed to anything? like errs errors and still get the same result..

and finally, how does it know to reference that particular ValueError?

thank you for all you help!!

2 Answers

Kyle Knapp
Kyle Knapp
21,526 Points

except ValueError as err simply assigns the alias of err to ValueError. You can use whatever alias you want.

except ValueError as flying_purple_elephants:
    print(flying_purple_elephants)

will work just the same.

except ValueError will always catch the first ValueError it finds. If we wanted to catch type errors instead, we would use except TypeError, which will always catch the first TypeError it finds. except always catches the first error of the specified type that it finds by default, that's how it "knows" which value to catch.

Dylan Bailey
Dylan Bailey
574 Points

Kyle is spot on with his answer.

The reason it's catching that error is because it's looking for the first error that's raised.

Since we are doing...

raise ValueError("More than 1 person is required to split the check")

When our code goes into...

except ValueError as err:

it snags that error we raised earlier.