Raising Exceptions4:45 with Craig Dennis
Let's learn how to raise our own exceptions to help prevent invalid values from being added.
Our split_check function here is currently allowing callers 0:00 of our function to pass in arguments that are invalid. 0:03 Now as it is, if number of people comes in as zero, 0:07 we end up opening up that worm hole with a zero division error. 0:09 Also, we end up allowing for a negative amount of people to split a check, 0:14 which makes it look like the restaurant actually owes us some money. 0:17 So, let's do this. 0:21 Let's verify that we have a good value, and 0:23 we if we don't, let's let the caller know that they've caused an exception. 0:25 So we need to make sure that our value is greater than zero. 0:29 Well, actually, wait a second. 0:34 Why would you split a check with one person? 0:36 That should probably not be allowed either. 0:38 Our math would work, but that's not the point. 0:40 So let's see, so right in here, right at the top, let's check. 0:43 Let's see if number_of_people is less than or equal to 1. 0:46 So in order to cause an exception to happen, you use the keyword raise, 0:50 and then you use the name of the exception that you want to raise. 0:56 And so we wanna raise a ValueError, right, because this is a bad thing. 1:02 So here we've got the ValueError. 1:06 I'm gonna copy and paste that. 1:08 So we have ValueError, so raise ValueError. 1:09 And then you can add additional arguments here, and 1:11 they will part of the exception when it's displayed, watch. 1:14 So we say, more than 1 person is required to 1:17 split the check, right, there we go. 1:22 So the way that raise works is as soon as this line is ran, 1:26 the function exits and the exception is bubbled up to the caller, 1:33 meaning it comes out of this and the caller has it. 1:38 So if the error is handled there, then the exception handling code will run. 1:41 And if not, the program ends and you see the trace back. 1:46 So let's review how this function is actually being called. 1:49 So we're trying to coerce values and we're catching a value error, 1:52 if that were to happen, and if it doesn't, this else block happens. 1:57 Okay, right, and this is where we call the split_check function. 2:01 So right now, this line is actually not in the try block, so 2:06 our error is currently unhandled. 2:10 So let's go ahead and run it and see what it looks like when the error is unhandled. 2:13 So python check please, I want 20. 2:19 And how many people? 2:22 There are zero, yikes. 2:23 But look, here's our ValueError, and 2:27 more than 1 person is required to split the check. 2:30 So since we're raising a ValueError, our try block is already handling, so 2:34 what we could do, is we could just move this line, 2:39 Up into here, and there we go, and now it will get caught. 2:44 So let's do that, let's try that one more time, I'm gonna clear this. 2:49 Python check please, 20 bucks, 0 people, no, that's not a valid value, try again. 2:53 We lost our messaging though, hm, that's a bummer. 3:01 Now, one thing you can do is to get a reference to the exception that was 3:04 raised. 3:09 And you can do this with a new keyword that's called as, so 3:09 you say except ValueError as error, we'll just call it err. 3:14 It's a new variable that will be created, and 3:19 it will be assigned the exception that was thrown. 3:21 So now, we can just print that out. 3:23 For instance, one thing that we could do is just to write it out 3:27 in a new message to the console, so let's do that. 3:30 Let's surround our error with some placeholders there, so 3:33 we'll say .format, and we'll push in the error. 3:38 There we go. 3:44 And if we run this again, 20, 0, there's our 3:46 message inside the parentheses, more than 1 person is required to split the check. 3:53 It's pretty clean, right? 3:58 There we go. 4:00 Awesome, we've now communicated very clearly to users of your function 4:01 that they need at least two people to split a check. 4:05 And even if they try something funny like giving a negative amount of people, 4:08 our calculation code won't even run. 4:12 We'll raise an exception before it gets there. 4:14 When I say users of your function, I wanna remind you that you are going to write 4:17 code that other people are going to want to use. 4:20 Being thoughtful and 4:24 explicit about your exceptions that you raise can really help your fellow coders. 4:24 If you, or someone else, ever wants to call that function that you just created, 4:29 now that logic that protects it so that you can only split a check with two or 4:33 more people is actually encoded inside the function. 4:36 Other callers of that function won't have to write or 4:40 even think about that exception logic themselves. 4:42
You need to sign up for Treehouse in order to download course files.Sign up