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

Mark Nembhard
Mark Nembhard
1,387 Points

Logic of where the error is handled in the program. there seems to be 2 places

The example error in input for the total amount of people. I am confused over where it is handled? In the except block? Yes, but by simply adding the ValueError as err it seems to jump to the function block above then come back down to the except error block and then print both errors. Please clarify. the "as err". Is it that if a ValueError is raised, because it has that attribute("more than..."), will it use that punchline anytime it is activated even though it(the ValueErrror("More than..") is in the split_check function.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The flow when using 20 and 0 as inputs is:

  • define split_check function (lines 4-7)
  • run try block` (line 9)
    • get total input (line 10)
    • get people input (line 11)
    • call split_check (line 12)
  • running `split_check (line 4)
    • raise function is reached, ValueError exception (line 6)
    • execution returned to calling line (line 12)
  • ValueError caught by try section, jump to except block
  • except line creates a reference to the ValueError object with the name err (line 13)
    • print first message (line 14)
    • print text from err (line 15)

When an error is raised, an error object is created with attributes for the error type, message string, and traceback information. If printed, the error object returns the string used to define the error object back when ValueError() was called in line 6.

Post back if you need more help. Good luck!!