1 00:00:00,000 --> 00:00:02,970 In Python, when your code runs into an error, it does this 2 00:00:02,970 --> 00:00:04,900 thing called raising an exception, 3 00:00:04,920 --> 00:00:08,060 for the most part. In other languages, this is also called 4 00:00:08,060 --> 00:00:09,300 throwing an exception. 5 00:00:09,320 --> 00:00:12,170 Now, in Python, we have this little 'try-except' block. So 6 00:00:12,170 --> 00:00:19,600 we could 'try' some code, and then we could 'except' an error. 7 00:00:19,660 --> 00:00:23,560 So if our code fails for whatever reason, we can 'except' the 8 00:00:23,570 --> 00:00:25,120 error and gracefully handle it. 9 00:00:26,700 --> 00:00:30,100 Now, a good example of this is '1/0', you get 10 00:00:30,100 --> 00:00:31,600 a 'ZeroDivisionError'. 11 00:00:31,600 --> 00:00:35,200 Simply put, in mathematics, 1 cannot be divided by 0. 12 00:00:35,200 --> 00:00:40,800 And so if we do this, 'print("HELLLOOOOO")', 13 00:00:40,800 --> 00:00:43,440 notice that our 'print' statement does not show up. 14 00:00:43,440 --> 00:00:46,600 It caught an error on here and died. 15 00:00:46,600 --> 00:00:49,600 Now, like we see our 'print' statement in the error itself, 16 00:00:49,600 --> 00:00:51,810 but we don't actually see it printed out to the page. 17 00:00:52,040 --> 00:00:55,100 And so this is a really good reason why we want to catch 18 00:00:55,110 --> 00:00:59,430 errors and then gracefully handle them so that our program 19 00:00:59,490 --> 00:01:02,910 doesn't totally bug out and die, because that's a terrible 20 00:01:02,920 --> 00:01:05,790 user experience, and the first person they're going to blame 21 00:01:05,800 --> 00:01:06,800 is the programmer. 22 00:01:06,800 --> 00:01:10,190 So why is it important to be able to catch this particular 23 00:01:10,200 --> 00:01:12,100 error or any sort of error that happens? 24 00:01:12,180 --> 00:01:17,070 Well, (a) I don't want you to get blamed for anything, but (b) in 25 00:01:17,080 --> 00:01:20,200 Python, when it runs into an error it usually just dies. And 26 00:01:20,210 --> 00:01:23,560 see, errors are going to happen. 27 00:01:23,800 --> 00:01:29,100 There's just no way around it. If it has to do with working with humans, 28 00:01:29,100 --> 00:01:31,800 so maybe you're making a website, or maybe you're working 29 00:01:31,800 --> 00:01:35,990 with data, or you have something that has to do in relation 30 00:01:36,000 --> 00:01:37,900 to a human in any way, shape or form, 31 00:01:37,960 --> 00:01:39,220 you're going to get errors, 32 00:01:39,220 --> 00:01:42,800 and that's because humans come with user input, and user input, 33 00:01:42,800 --> 00:01:47,700 always, always, always is a string, and users, a lot of us, 34 00:01:47,700 --> 00:01:50,120 including myself, can be quite malicious. 35 00:01:50,120 --> 00:01:52,700 We try to break things just because we know we can. 36 00:01:52,700 --> 00:01:56,900 And so it's your job as a programmer to say, "Hey, yep, human, 37 00:01:56,990 --> 00:02:00,750 you are allowed to try to break this, but I'm going to catch 38 00:02:00,750 --> 00:02:03,300 that problem and I'm going to work with it anyways". 39 00:02:03,360 --> 00:02:06,660 So let's go ahead and show another error here. 40 00:02:06,670 --> 00:02:10,060 So let's say we've got a dictionary, and it's got a key in 41 00:02:10,070 --> 00:02:14,640 here 'name', and the value is going to be "Kalob". 42 00:02:14,640 --> 00:02:19,800 And then we try to do something like 'print(d[])', 43 00:02:19,800 --> 00:02:24,800 and then my 'hobby', which does not exist, this key in the 44 00:02:24,800 --> 00:02:26,300 dictionary simply does not exist. 45 00:02:26,300 --> 00:02:28,100 We're going to see a 'KeyError'. 46 00:02:28,100 --> 00:02:32,500 This one's got a 'ZeroDivisionError', and this one is a 'KeyError'. 47 00:02:32,500 --> 00:02:34,300 And there's all different types of errors. 48 00:02:34,320 --> 00:02:39,510 So when it comes to catching errors, there are a few keywords 49 00:02:39,520 --> 00:02:43,900 we want to keep our eyes out for. 'try' is one of them, 'except' 50 00:02:43,900 --> 00:02:47,200 is another one, and 'finally' is the last one. 51 00:02:47,200 --> 00:02:52,800 Now, what this is going to do, is it's going to 'try' to execute the code, 52 00:02:52,880 --> 00:02:54,620 if it runs into an error, 53 00:02:54,620 --> 00:02:58,400 if it runs into some sort of exception, it's going to exit 54 00:02:58,400 --> 00:03:03,000 the 'try' block immediately, and it's going to try to handle 55 00:03:03,000 --> 00:03:10,400 the error, and then 'finally' will always run no matter what. 56 00:03:10,400 --> 00:03:14,300 So let's go ahead and give this a quick little demo. 57 00:03:15,300 --> 00:03:20,900 Let's 'try: total = 10/0', and we're 58 00:03:20,900 --> 00:03:24,800 going to 'except' that there could possibly be an error in 59 00:03:24,800 --> 00:03:26,400 there, a 'ZeroDivisionError', 60 00:03:26,400 --> 00:03:29,400 so we're going to 'except: ZeroDivisionError', 61 00:03:30,800 --> 00:03:33,900 and if that happens, we're going to say the 'total = 'N/A'', 62 00:03:33,900 --> 00:03:36,500 it's not applicable, just can't be done, 63 00:03:36,530 --> 00:03:39,800 and 'finally:' we will say 'print( 64 00:03:39,800 --> 00:03:39,810 "The total is", total)', 65 00:03:43,200 --> 00:03:45,600 [no audio] 66 00:03:45,600 --> 00:03:46,900 and it says, "The total is N/A". 67 00:03:47,610 --> 00:03:51,320 So it tried this, failed, and then went over here and said, 68 00:03:51,330 --> 00:03:52,970 "Okay, we'll set the variable. 69 00:03:52,970 --> 00:03:54,200 total = 'N/A'", 70 00:03:54,600 --> 00:03:56,400 and then finally it ran some code. 71 00:03:56,400 --> 00:03:58,560 Now, if you're doing something like this, technically, you 72 00:03:58,560 --> 00:04:01,100 don't really need the 'finally' in there. You can run this, 73 00:04:01,100 --> 00:04:02,700 and it works just as fine. 74 00:04:02,720 --> 00:04:05,360 But I'm going to undo that, because that is a good little 75 00:04:05,370 --> 00:04:07,440 example. Let's try another one. 76 00:04:07,440 --> 00:04:11,800 Let's say we've got 'number1 = 50', and 77 00:04:11,800 --> 00:04:13,800 'number2' is going to be user input. 78 00:04:13,800 --> 00:04:19,899 So 'input("Enter a number:")', any number, and then we're going 79 00:04:19,910 --> 00:04:22,000 to print the total. 80 00:04:22,000 --> 00:04:25,600 So the 'total' is going to be 'number1 + number2', 81 00:04:25,600 --> 00:04:27,800 [no audio] 82 00:04:27,800 --> 00:04:29,600 and 'print(total)'. 83 00:04:29,680 --> 00:04:34,040 "Enter a number", 123, and we get a different type of error. 84 00:04:34,040 --> 00:04:35,400 We get a 'TypeError'. 85 00:04:35,440 --> 00:04:39,580 Now, the reason for that is because 'number1', this is an 86 00:04:39,590 --> 00:04:41,550 int, 'number2', this is a string. 87 00:04:41,550 --> 00:04:45,100 Well, how are you going to add numbers to letters? 88 00:04:45,100 --> 00:04:46,500 That's really hard to do. 89 00:04:46,500 --> 00:04:49,100 Now, even though I entered a proper number, again a program 90 00:04:49,100 --> 00:04:51,100 does not know that this came back as a string. 91 00:04:51,160 --> 00:04:56,500 And so Python said, "You are trying to add letters to a number, 92 00:04:56,510 --> 00:04:57,910 simply cannot do that". 93 00:04:57,910 --> 00:05:03,600 But what we can do instead, is we can 'try' to do this, and 94 00:05:03,600 --> 00:05:08,070 then we can 'except' that there is some sort of error in here, 95 00:05:08,070 --> 00:05:11,000 and we can say 'print("There was an error")'. 96 00:05:11,000 --> 00:05:15,500 And let's go ahead and move that 'print(total)' up here, and rerun this. 97 00:05:15,500 --> 00:05:19,600 "Enter a number: 456, and it just says, "There was an error". 98 00:05:19,690 --> 00:05:22,330 And so now our code isn't complaining. 99 00:05:22,330 --> 00:05:23,800 It's not doing this. 100 00:05:23,860 --> 00:05:26,440 It's actually saying, "Hey, this doesn't work". 101 00:05:26,800 --> 00:05:29,700 The total was not printed, but the error was printed. 102 00:05:29,700 --> 00:05:32,000 And that means, 'print( 103 00:05:32,600 --> 00:05:37,700 "The program can continue without problems")'. 104 00:05:37,700 --> 00:05:41,200 And sure enough, "The program can continue without problems". 105 00:05:41,200 --> 00:05:43,930 Now, in our first example, that 'print' statement no longer 106 00:05:43,930 --> 00:05:45,700 worked. It died right here. 107 00:05:45,700 --> 00:05:47,680 Python just said, "I can't figure this out", 108 00:05:47,690 --> 00:05:50,780 and so this was never executed. Down here 109 00:05:50,780 --> 00:05:53,200 Python said, "Hey, I can't figure this out", 110 00:05:53,200 --> 00:05:54,600 it broke out of this 111 00:05:54,600 --> 00:05:58,670 'try' block, did not execute this, and executed the 'except' code 112 00:05:58,730 --> 00:06:02,610 instead. Now, we've actually seen a lot of different error 113 00:06:02,620 --> 00:06:06,160 types throughout this course, and I think we've seen three 114 00:06:06,170 --> 00:06:07,960 of them in this particular lesson. 115 00:06:07,970 --> 00:06:10,630 So in the next lesson, I'm going to teach you how to discover 116 00:06:10,800 --> 00:06:14,700 what the error types are and how you can handle them.