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

kelsang sherab
kelsang sherab
749 Points

In 3:19 it says err is a new variable. How come?

In 3:19 Craig says that err is a new variable created and assigned the value "More than 1 person etc ..." how does it know where to take its assigned value?

3 Answers

err is assigned to the error's message.

Back in the split_check function, Craig raises an error with the message "More than 1 person is required to split the check". In the except block, Craig is catching that error and extracting the message out of the error. The except ValueError as err tells Python to catch any ValueErrors and put its error message in a new variable called err.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

More info here Python Exceptions Handling

Check the "Argument of an Exception" section

Beginning of the text: An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary by exception. You capture an exception's argument by supplying a variable in the except clause as follows βˆ’

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Great question!

In the try documentation:

When a matching except clause is found, the exception is assigned to the target specified after the as keyword in that except clause, if present, and the except clause’s suite is executed.

The identifier, err in this case, will hold the exception object so that it can be referenced from within the except clause's suite (code block).

Post back if you need more details. Good Luck!!

kelsang sherab
kelsang sherab
749 Points

Thanks all. As a beginner I find it all a little bit much to digest quickly. I feel I need to spend time with the code and your answers.

Thanks for helping!!