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 trialNiki Nikiforidi
449 PointsIf the code is interrupted, how does raise get run
In the video Raising Expectations, at about 3:55 seconds, he inputs the value 0 for How Many People?, and then the error massage we saved in the function we created was displayed. If we never called split_check, how did python know to display that sentence?
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsHey Niki Nikiforidi, good question. Here’s the flow when a 0 is entered:
- line 11, zero is entered
- line 12, function
split_check
called with parameters (20, 0) - line 4, function begins running within
total=20, number_of_people=0
- line 5, value of
number_of_people
checked if less than or equal to one. check fails - line 6, ValueError is raised, error object string set to “More than 1 person is required to split the check”, function returns error. Return to line 12
-
try
block lines 10-12, has error. Jump to line 13 - line 13, if error raised is of type
ValueError
, seterr
to error object returned, move to line 14 - line 14, print first string
- line 15, print “More than...” error message from raised error that was set in line 6
Post back if you need more help. Good luck!!!
Niki Nikiforidi
449 PointsThank you for your response. I'm confused how from line 11 it then goes to line 12. From the previous lectures we have been taught that once an error occurs, python leave the line it was at and goes straight to line 13. When is line 12 even touched if at line 11 there is an error?
Chris Freeman
Treehouse Moderator 68,441 PointsYou are correct that the flow changes when an error occurs or is explicitly raised. In this case, the error does get raised until line 6 in the flow shown in the other answer.
The assignment to the module-level defined number_of_people
in line 11 triggers nothing, so flow naturally flows to line 12.
It is only after the local parameter number_of_people
inside of the function split_check
is set to point at the same object as the module-level number_of_people
points to and then this local variable is checked in line 5 that an error is raised.
Until the error is actually raised, flow moves normally.