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

Sarah Toukan
Sarah Toukan
1,079 Points

Is ValueError a variable containing an object? How does the program remember the message contained in it?

I'm try to run through the program at 3:58 through the eyes of the interpreter, when 0 is input as the answer to "How many people?".

The function is called within the try part of the block, and the error is raised:

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

Is this what is returned from the function? I'm asking because afterwards, we enter the except part of the block, and we're telling the program to name our ValueError as err.

I'm guessing the program remembered what was in ValueError outside of the function because that's what was "returned"?

1 Answer

Steven Parker
Steven Parker
231,271 Points

A ValueError is an object, which is an extension of the more generic object Exception. These objects have an attribute named "message" which contains text describing the issue. When you create an exception object, you can set the message attribute by passing in a string argument.

Then, when an Exception is converted (or implicitly coerced) into a string, it returns the contents of the "message" attribute.

And an Exception that is raised can optionally be caught by an "except" statement into a variable (such as "err").

For more details, see the chapter on Exceptions in the Python documentation.