1 00:00:01,310 --> 00:00:06,485 Murphy's Law states, anything that can go wrong will go wrong. 2 00:00:06,485 --> 00:00:09,580 Now, that's a pretty negative way of looking at the world. 3 00:00:09,580 --> 00:00:13,640 But it's a pretty good way to think about how users might use your application. 4 00:00:13,640 --> 00:00:16,080 If they can break it, they will. 5 00:00:16,080 --> 00:00:20,040 We've seen some syntax errors getting thrown when write improper code. 6 00:00:20,040 --> 00:00:21,280 But as we just witnessed, 7 00:00:21,280 --> 00:00:25,130 our users can also create errors as our program is running. 8 00:00:25,130 --> 00:00:27,500 When they don't do what we expected them to do, 9 00:00:27,500 --> 00:00:30,130 these types of errors are called exceptions. 10 00:00:31,200 --> 00:00:32,530 The experience for a user, 11 00:00:32,530 --> 00:00:35,800 when we run into one of these exceptions, can be pretty intense. 12 00:00:35,800 --> 00:00:38,880 There's a stack trace and some of our code even, and a message and 13 00:00:38,880 --> 00:00:40,160 our program didn't even finish. 14 00:00:40,160 --> 00:00:42,960 It just stopped right there on the line that caused the error. 15 00:00:42,960 --> 00:00:44,980 We shouldn't show that to the user. 16 00:00:44,980 --> 00:00:48,030 I'm sure you've seen an application where the error wasn't handled properly.. 17 00:00:49,060 --> 00:00:53,440 The good news is that there is a way to handle these exceptions gracefully. 18 00:00:53,440 --> 00:00:56,746 Let's see if we can't clean up those exceptions that we just saw. 19 00:00:56,746 --> 00:00:59,740 So let's make our user facing exception happen again. 20 00:00:59,740 --> 00:01:00,880 Let's kick off the program. 21 00:01:00,880 --> 00:01:04,574 So we'll say, python check_please. 22 00:01:06,470 --> 00:01:08,660 And what was the total value? 23 00:01:08,660 --> 00:01:10,520 It was too much. 24 00:01:11,910 --> 00:01:15,510 Now obviously we can't coerce that saying into a float. 25 00:01:15,510 --> 00:01:17,780 So let's look what the error is though. 26 00:01:17,780 --> 00:01:20,530 We definitely don't want the user to see this right? 27 00:01:20,530 --> 00:01:22,190 Can you imagine seeing this? 28 00:01:22,190 --> 00:01:23,680 What is it traceback? 29 00:01:23,680 --> 00:01:26,630 I just want to split my check, so let's fix it. 30 00:01:26,630 --> 00:01:31,090 If you have some code that you think might cause for some exceptional activity like 31 00:01:31,090 --> 00:01:35,320 any of this type coercion stuff, any of that, it's great for this sort of thing. 32 00:01:35,320 --> 00:01:39,060 You can put it in what is known as a try. 33 00:01:39,060 --> 00:01:40,550 So try is a statement. 34 00:01:40,550 --> 00:01:44,790 So let's do that, we'll say try and of course, that's followed by a block. 35 00:01:46,250 --> 00:01:47,970 So we need a colon. 36 00:01:47,970 --> 00:01:52,850 And what you do is you put your possibly problematic code in that try block. 37 00:01:52,850 --> 00:01:55,085 So here let's move these in. 38 00:01:55,085 --> 00:01:58,700 I'm just going to tab this in, tab this in. 39 00:01:58,700 --> 00:02:02,510 And now what we want to do is write the code that handles 40 00:02:02,510 --> 00:02:04,098 that this exception has happened. 41 00:02:04,098 --> 00:02:10,565 So the keyword that we want to use is except. 42 00:02:10,565 --> 00:02:15,400 So we say except, now as you know, there are different type of errors. 43 00:02:15,400 --> 00:02:18,350 So you want to catch just this one error that we are doing now. 44 00:02:18,350 --> 00:02:21,085 And as we saw, it's a value error. 45 00:02:21,085 --> 00:02:25,410 I'm gonna go ahead and I'm gonna right click, copy and I wanna paste it here. 46 00:02:25,410 --> 00:02:28,500 And then I wanna put a colon. 47 00:02:29,500 --> 00:02:32,944 And now, the body of this is what we'll run 48 00:02:32,944 --> 00:02:36,399 should this specific exceptions happen. 49 00:02:36,399 --> 00:02:38,712 So what do we want to say? 50 00:02:38,712 --> 00:02:44,150 Well, let's keep it user friendly, well say print, how about no, 51 00:02:44,150 --> 00:02:48,420 that's not a valid value, try again. 52 00:02:50,970 --> 00:02:52,910 Feel like I've seen that before, right? 53 00:02:52,910 --> 00:02:55,200 All right, now let's see what we got. 54 00:02:57,600 --> 00:02:59,770 Python check please. 55 00:02:59,770 --> 00:03:00,430 What's the total? 56 00:03:00,430 --> 00:03:01,730 Too much. 57 00:03:03,600 --> 00:03:06,320 There's our message, no, that's not a valid value please try again? 58 00:03:06,320 --> 00:03:09,950 But then we got a traceback again. 59 00:03:09,950 --> 00:03:11,750 Hmm, but look, this is different. 60 00:03:11,750 --> 00:03:12,335 This is a name error. 61 00:03:12,335 --> 00:03:16,730 And this says name total due is not defined. 62 00:03:16,730 --> 00:03:17,830 It's weird, it's right here. 63 00:03:18,990 --> 00:03:20,400 Now in order to understand this, 64 00:03:20,400 --> 00:03:23,675 we need to figure out what's happening inside this try block. 65 00:03:23,675 --> 00:03:27,905 So, we came in to the try block and what happened was we asked for 66 00:03:27,905 --> 00:03:32,875 this input and we returned a string and then the float tried to coerce that. 67 00:03:32,875 --> 00:03:38,042 And that raised an exception, it raised a value ValueError exception, and 68 00:03:38,042 --> 00:03:41,232 so what happened was this whole line, never ran. 69 00:03:41,232 --> 00:03:44,912 This float line ran, but it didn't return because it hit the exception, so 70 00:03:44,912 --> 00:03:46,882 it popped into here and it wrote this. 71 00:03:46,882 --> 00:03:51,802 So what happened was this assignment never actually occurred, it never happened. 72 00:03:51,802 --> 00:03:55,942 So when we get down here, we're talking about total_due, and 73 00:03:55,942 --> 00:03:57,965 it was never assigned. 74 00:03:57,965 --> 00:04:01,050 Aha, name error, what you talking about? 75 00:04:01,050 --> 00:04:05,220 So what we really want is we only want this code to run, 76 00:04:05,220 --> 00:04:08,000 this amount due and actually the print statement for that matter. 77 00:04:08,000 --> 00:04:14,030 We only want that to run if and only if there was no exceptions. 78 00:04:14,030 --> 00:04:18,310 So the try block also allows for what's known as an else. 79 00:04:18,310 --> 00:04:19,800 Just like we saw in an if statement. 80 00:04:19,800 --> 00:04:20,300 So let's do that. 81 00:04:20,300 --> 00:04:21,290 So we'll say else. 82 00:04:22,430 --> 00:04:24,560 And then that's a block and we can bring these up. 83 00:04:26,070 --> 00:04:31,986 So this can be read like try this 84 00:04:31,986 --> 00:04:36,660 code, except if there is a value error. 85 00:04:36,660 --> 00:04:38,170 In which case run this code. 86 00:04:39,410 --> 00:04:42,240 Otherwise if everything's great, run this code. 87 00:04:44,220 --> 00:04:47,590 Now you might be thinking that I definitely could have put this code 88 00:04:47,590 --> 00:04:49,420 just up here in the try statement. 89 00:04:49,420 --> 00:04:51,680 I didn't actually need this else. 90 00:04:51,680 --> 00:04:55,890 But what I'm communicating to readers of this particular code is that 91 00:04:55,890 --> 00:04:59,220 this code here is expecting an error. 92 00:04:59,220 --> 00:05:00,120 If I put this up here, 93 00:05:00,120 --> 00:05:03,790 I'm not really worried about those throwing a value error at the moment. 94 00:05:03,790 --> 00:05:07,800 So this is specifically saying these might throw a value error. 95 00:05:09,270 --> 00:05:09,770 So what do you say? 96 00:05:09,770 --> 00:05:11,056 Let's try it again, let's see how we did. 97 00:05:11,056 --> 00:05:12,733 Check please. 98 00:05:13,916 --> 00:05:16,560 Too much. 99 00:05:16,560 --> 00:05:17,281 There we go. 100 00:05:17,281 --> 00:05:18,794 No, that's not a valid value, try again. 101 00:05:18,794 --> 00:05:19,980 Got it. 102 00:05:21,550 --> 00:05:26,610 All right, so we now have our coercion error handling working pretty smooth. 103 00:05:26,610 --> 00:05:29,900 Let's see what happens now with our zero error. 104 00:05:29,900 --> 00:05:32,350 So again, we want python check_please. 105 00:05:33,620 --> 00:05:34,510 What's the total? 106 00:05:34,510 --> 00:05:37,946 It was 20 bucks and there were zero people. 107 00:05:37,946 --> 00:05:42,130 Okay, so now we still have that zero division error. 108 00:05:42,130 --> 00:05:45,900 So we could actually handle multiple exception types. 109 00:05:45,900 --> 00:05:50,457 So one thing that you could do is you could just say accept 110 00:05:50,457 --> 00:05:58,020 ZeroDivisionError and then I'd write the code here, right? 111 00:05:58,020 --> 00:06:02,580 Write some code here to do write exception handling code. 112 00:06:02,580 --> 00:06:06,510 But, wait a second, I'm gonna undo that. 113 00:06:06,510 --> 00:06:10,154 So let's undo this, Cmd+Z again, undo. 114 00:06:12,640 --> 00:06:18,370 I wanna note that the exception is coming from within our split_check method. 115 00:06:18,370 --> 00:06:21,980 I could wrap this math.ceil code in a try and an except block. 116 00:06:21,980 --> 00:06:28,880 But actually, if you think about it, zero is an invalid value for this function. 117 00:06:28,880 --> 00:06:31,720 You cant split a check with zero people. 118 00:06:31,720 --> 00:06:34,580 Actually you know what else, hold on a second. 119 00:06:34,580 --> 00:06:38,623 What happens if we say python check_please, 120 00:06:38,623 --> 00:06:42,600 20 bucks, and we're going to split that with negative 12 people. 121 00:06:42,600 --> 00:06:47,280 Each person owes $-1, so does that mean the restaurant owes us a dollar? 122 00:06:47,280 --> 00:06:49,060 You can't split a check with negative 12 people, 123 00:06:49,060 --> 00:06:51,030 you can't even have negative 12 people. 124 00:06:51,030 --> 00:06:56,250 So we really should consider less than or equal to zero as an exception. 125 00:06:56,250 --> 00:06:59,790 And we should raise that exception so that others could catch it. 126 00:06:59,790 --> 00:07:01,660 Just like this called a float did right? 127 00:07:01,660 --> 00:07:06,330 Float said, hey I can't do that I'm gonna raise an exception and then you handle it. 128 00:07:06,330 --> 00:07:07,030 You know what we should do? 129 00:07:07,030 --> 00:07:07,750 We should do that too. 130 00:07:07,750 --> 00:07:10,430 We should make our split check raise an exception. 131 00:07:10,430 --> 00:07:12,360 It seems like good software design right? 132 00:07:12,360 --> 00:07:15,250 Let's take a look at how to raise exceptions right after this quick break.