1 00:00:00,482 --> 00:00:03,790 Our split_check function here is currently allowing callers 2 00:00:03,790 --> 00:00:07,080 of our function to pass in arguments that are invalid. 3 00:00:07,080 --> 00:00:09,820 Now as it is, if number of people comes in as zero, 4 00:00:09,820 --> 00:00:14,280 we end up opening up that worm hole with a zero division error. 5 00:00:14,280 --> 00:00:17,890 Also, we end up allowing for a negative amount of people to split a check, 6 00:00:17,890 --> 00:00:21,390 which makes it look like the restaurant actually owes us some money. 7 00:00:21,390 --> 00:00:23,480 So, let's do this. 8 00:00:23,480 --> 00:00:25,290 Let's verify that we have a good value, and 9 00:00:25,290 --> 00:00:29,750 we if we don't, let's let the caller know that they've caused an exception. 10 00:00:29,750 --> 00:00:33,570 So we need to make sure that our value is greater than zero. 11 00:00:34,790 --> 00:00:36,360 Well, actually, wait a second. 12 00:00:36,360 --> 00:00:38,990 Why would you split a check with one person? 13 00:00:38,990 --> 00:00:40,570 That should probably not be allowed either. 14 00:00:40,570 --> 00:00:43,410 Our math would work, but that's not the point. 15 00:00:43,410 --> 00:00:46,580 So let's see, so right in here, right at the top, let's check. 16 00:00:46,580 --> 00:00:50,878 Let's see if number_of_people is less than or equal to 1. 17 00:00:50,878 --> 00:00:56,345 So in order to cause an exception to happen, you use the keyword raise, 18 00:00:56,345 --> 00:01:01,279 and then you use the name of the exception that you want to raise. 19 00:01:02,570 --> 00:01:06,800 And so we wanna raise a ValueError, right, because this is a bad thing. 20 00:01:06,800 --> 00:01:08,110 So here we've got the ValueError. 21 00:01:08,110 --> 00:01:09,480 I'm gonna copy and paste that. 22 00:01:09,480 --> 00:01:11,686 So we have ValueError, so raise ValueError. 23 00:01:11,686 --> 00:01:14,150 And then you can add additional arguments here, and 24 00:01:14,150 --> 00:01:17,640 they will part of the exception when it's displayed, watch. 25 00:01:17,640 --> 00:01:22,108 So we say, more than 1 person is required to 26 00:01:22,108 --> 00:01:26,930 split the check, right, there we go. 27 00:01:26,930 --> 00:01:32,160 So the way that raise works is as soon as this line is ran, 28 00:01:33,340 --> 00:01:38,660 the function exits and the exception is bubbled up to the caller, 29 00:01:38,660 --> 00:01:41,490 meaning it comes out of this and the caller has it. 30 00:01:41,490 --> 00:01:46,100 So if the error is handled there, then the exception handling code will run. 31 00:01:46,100 --> 00:01:49,790 And if not, the program ends and you see the trace back. 32 00:01:49,790 --> 00:01:52,960 So let's review how this function is actually being called. 33 00:01:52,960 --> 00:01:57,640 So we're trying to coerce values and we're catching a value error, 34 00:01:57,640 --> 00:02:01,500 if that were to happen, and if it doesn't, this else block happens. 35 00:02:01,500 --> 00:02:04,930 Okay, right, and this is where we call the split_check function. 36 00:02:06,230 --> 00:02:10,490 So right now, this line is actually not in the try block, so 37 00:02:10,490 --> 00:02:13,220 our error is currently unhandled. 38 00:02:13,220 --> 00:02:19,040 So let's go ahead and run it and see what it looks like when the error is unhandled. 39 00:02:19,040 --> 00:02:22,029 So python check please, I want 20. 40 00:02:22,029 --> 00:02:23,890 And how many people? 41 00:02:23,890 --> 00:02:27,230 There are zero, yikes. 42 00:02:27,230 --> 00:02:30,280 But look, here's our ValueError, and 43 00:02:30,280 --> 00:02:32,780 more than 1 person is required to split the check. 44 00:02:34,560 --> 00:02:39,313 So since we're raising a ValueError, our try block is already handling, so 45 00:02:39,313 --> 00:02:42,393 what we could do, is we could just move this line, 46 00:02:44,359 --> 00:02:49,357 Up into here, and there we go, and now it will get caught. 47 00:02:49,357 --> 00:02:53,748 So let's do that, let's try that one more time, I'm gonna clear this. 48 00:02:53,748 --> 00:02:59,870 Python check please, 20 bucks, 0 people, no, that's not a valid value, try again. 49 00:03:01,080 --> 00:03:04,803 We lost our messaging though, hm, that's a bummer. 50 00:03:04,803 --> 00:03:09,196 Now, one thing you can do is to get a reference to the exception that was 51 00:03:09,196 --> 00:03:09,804 raised. 52 00:03:09,804 --> 00:03:14,389 And you can do this with a new keyword that's called as, so 53 00:03:14,389 --> 00:03:19,267 you say except ValueError as error, we'll just call it err. 54 00:03:19,267 --> 00:03:21,262 It's a new variable that will be created, and 55 00:03:21,262 --> 00:03:23,960 it will be assigned the exception that was thrown. 56 00:03:23,960 --> 00:03:27,310 So now, we can just print that out. 57 00:03:27,310 --> 00:03:30,740 For instance, one thing that we could do is just to write it out 58 00:03:30,740 --> 00:03:33,360 in a new message to the console, so let's do that. 59 00:03:33,360 --> 00:03:38,164 Let's surround our error with some placeholders there, so 60 00:03:38,164 --> 00:03:42,316 we'll say .format, and we'll push in the error. 61 00:03:44,458 --> 00:03:45,040 There we go. 62 00:03:46,220 --> 00:03:53,398 And if we run this again, 20, 0, there's our 63 00:03:53,398 --> 00:03:58,670 message inside the parentheses, more than 1 person is required to split the check. 64 00:03:58,670 --> 00:04:00,090 It's pretty clean, right? 65 00:04:00,090 --> 00:04:01,360 There we go. 66 00:04:01,360 --> 00:04:05,460 Awesome, we've now communicated very clearly to users of your function 67 00:04:05,460 --> 00:04:08,500 that they need at least two people to split a check. 68 00:04:08,500 --> 00:04:12,200 And even if they try something funny like giving a negative amount of people, 69 00:04:12,200 --> 00:04:14,180 our calculation code won't even run. 70 00:04:14,180 --> 00:04:17,290 We'll raise an exception before it gets there. 71 00:04:17,290 --> 00:04:20,760 When I say users of your function, I wanna remind you that you are going to write 72 00:04:20,760 --> 00:04:24,030 code that other people are going to want to use. 73 00:04:24,030 --> 00:04:24,860 Being thoughtful and 74 00:04:24,860 --> 00:04:29,480 explicit about your exceptions that you raise can really help your fellow coders. 75 00:04:29,480 --> 00:04:33,270 If you, or someone else, ever wants to call that function that you just created, 76 00:04:33,270 --> 00:04:36,990 now that logic that protects it so that you can only split a check with two or 77 00:04:36,990 --> 00:04:40,120 more people is actually encoded inside the function. 78 00:04:40,120 --> 00:04:42,240 Other callers of that function won't have to write or 79 00:04:42,240 --> 00:04:44,550 even think about that exception logic themselves.