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

Tyler Dix
Tyler Dix
14,230 Points

I don't understand why exceptions are handled two different ways

After staring at my code and trying to understand how Python is interpreting it, I'm confused. Here are my questions:

Why is the first exception handled in the "try" block, while the second exception is handled in the function? Note that using an identical variable assignment confused me, so I appended the variables in the function with "_def".

How are these two pieces of code "linked":

     if number_of_people_def <= 1:
        raise ValueError("More than 1 person is required to split the check")

and

    except ValueError as err:
    print("Oh no! That's not a valid value. Try again!")
    print("({})".format(err))

Can all exceptions be handled in the function? I tried this:

    if number_of_people_def <= 1:
        raise ValueError("More than 1 person is required to split the check")
    elif total_def != float:
        print("Enter a number please.")

Which leads me to my original question about how these are linked. If anyone can help shed light on this questions, I would really appreciate it.

Hi, your code is hard to interpret the way you have it. I see you tried to put them in code blocks, but I think you used single quotes ' instead of back tics `. Back tic is located on the top left corner under the esc key on my Mac. Not sure about your keyboard, but it's like the single quote backwards.

1 Answer

The function raises the error. The try block runs the function. If, while running the function, it gets the error that you specified (in this case ValueError), it exits the function and goes to the except block. The except block handles the error by printing a helpful message to the user. You can anticipate some errors in the function, but errors that happen during execution (exceptions) need to be handled outside the function.

Tyler Dix
Tyler Dix
14,230 Points

I think that makes sense. I just need more practice understanding how my code is understood by Python, and in what order. As I understand it, if the value type doesn't match, the "try" block catches it and prints one error. If the value type matches, but doesn't meet the condition defined in the function, then it prints whatever is assigned to the "err" function. In other words, if the value is a non-integer this program prints "Not a value, try again." And if the input is an integer, this passes the "try" block and then runs through the function, where it raises the ValueError. One exception form of error handling catches the error before it runs through the function, and the other form of exception handling catches the error as it runs through the function. Thank you, ursaminor.