Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Raising Exceptions

If 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
MOD
Chris Freeman
Treehouse Moderator 67,515 Points

Hey 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, set err 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!!!

Thank 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
Chris Freeman
Treehouse Moderator 67,515 Points

You 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.