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

Youssef Moustahib
Youssef Moustahib
7,779 Points

I have a question to RAISE

I don't quite understand this video. What was the point of using raise? Couldn't we just have typed the "you need more than 1 person to split the check" under the VALUEERROR exception and left it how it was?

4 Answers

Artjom Dzug
Artjom Dzug
8,238 Points

I am too really confused on this matter, i mean there are too much examples and not much explanations, i belive people want to hear a real explanation on like what it is and where you can use it, not like in this particular example

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Raise is a way to notify your user that you have caused an exception but not in the default way that the Python interpreter does. That's what the video is trying to teach you.

And in order to cause individual exceptions.... you write the code and then use the raise keyword. You add the Exception as a parameter and to associate it with your try block use a variable with the as keyword.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Imagine other code using the split_check function. By including the exception pattern, anyone who uses that function, not just our main code, will benefit.

This is logic that can be expressed within the function, and in all cases is what should happen, whenever that function is used. If we don't do it, everyone using the function will need to think about that exceptional behavior themselves.

This is really more about code reuse than anything else. Oftentimes you'll create your own Exception type and use it to explain to others as to how to use objects that you are defining.

I definitely am bending the example in our case to show how to raise an exception.

That clear things up?

Here is the official tutorial on errors and exceptions.

Youssef Moustahib
Youssef Moustahib
7,779 Points

Hi Craig,

It's still a little foggy.

Here is some code I ran:

My code is exactly like yours, but when I type a letter instead of a number, I get this message:

What is the final sum? 20 What is the total number of people? e oh no, thats not a valid value (could not convert string to float: 'e'). <-------------STRANGE MESSAGE??

I did not include that in my raise message?

Youssef Moustahib
Youssef Moustahib
7,779 Points

So what you are saying is that you have included the raise exception, to add in other exceptions so that when other coders see your code, they won't have to work out what the exceptions will be?

Craig Dennis
Craig Dennis
Treehouse Teacher

That "strange" exception is what happens when you create a coercion error, a TypeError.

That's the Exception thrown from here:

int('e')

Since it is a TypeError, your except block is "catching" it.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I've been struggling with this myself in recent days. So answering this question will hopefully help us both out. But there is a discernable difference.

Let's look at the script in question.

check_please.py
import math

def split_check(total, num_people):
    if num_people <= 1:
        raise ValueError("More than 1 person is required to split the check")
    return math.ceil(total / num_people)


try:
    total_due = float(input("What is the total?   "))
    num_people = int(input("How many people?   "))
    amount_due = split_check(total_due, num_people)

except ValueError as err:
    print("Sorry, this is not a valid value.")
    print("({})".format(err))

else:
    #run code if no exceptions
    print("Each person owes ${}".format(amount_due))

When you're using a try catch block, you're checking for code that might fail and checking it against a particular type of exception, and then coercing the interpreter to display the error in a more user friendly way.

So we use raise to catch what is specifically wrong... but with try we're catching the code that won't run because something has gone wrong.

In this case, someone has put a value in that doesn't work for the program.

Youssef Moustahib
Youssef Moustahib
7,779 Points

hmm, could we not handle it with an, if else block?

while true:
    if num_people <= 1:
        print("More than 1 person is required to split the check")
        continue

Craig Dennis could you help shed some light on this?

Thanks