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

Why is amount_due moved up into the try block?

I don't understand why exactly amount_due is moved up into the try block.

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

The reason the amount_due calculation has to move INSIDE the "try..except" block is that the function split_check was modified to raise an error. If the split_check raised an error outside of this block, the error will not be caught and Python will stop the execution of the program and show the error that was raised.

Konrad Gołda
Konrad Gołda
735 Points

But why, after ValueError handled in a function ocurred, this "else" block is not in the "try" block anymore? Like....really why? It was not explained in the video and not that obvious to get it as you may think ;)

Below I will write down my theory which I figured out after a few days (omfg) so it was wasted few days because I wanted to move one only when I get it and until I'm sure of my understanding (BTW I was learning only for about 1h per day because believe me... It's so annoying when you cannot get a short 5 min video after 1h of attempting to understsand it). Could anyone check it and tell me if this is a true?

So! This line where function is called has to be moved to the "try" block in order to execute handling of an raised exception (which I understand ValueError in def block is) in case it occurs? And if it occurs, this line has to be in this "try" block also because of order of operations and to execute "except" block? Am I right or wrong? Thank you!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

I'd have to look at the actual code to comment further.

The basic theory is that if an exception is raised outside of a try block, it will stop the execution of the Python script. During development, interrupted execution is desirable since you want to make sure nothing is hidden and that eventually, your program will handle expected errors, but provide enough information if/and when an unexpected error/boundary condition occurs.

In the final production program, you hope that the code will handle any and all errors gracefully. But nothing is ever perfect!