1 00:00:00,330 --> 00:00:07,740 An unavoidable part of being a programmer is that your programs are going to give a lot of errors, 2 00:00:08,520 --> 00:00:13,200 you can't avoid bugs, you can't avoid programs breaking. 3 00:00:14,180 --> 00:00:22,820 A simple print function here with, let's say, a misspelled word with let's say no comma, if I run 4 00:00:22,820 --> 00:00:25,630 this, we've been seeing this a lot, right? 5 00:00:25,910 --> 00:00:32,060 We've been seeing a lot of this red throughout this course, a lot of these errors. 6 00:00:32,780 --> 00:00:36,800 If I tried to add one plus let's say. 7 00:00:41,260 --> 00:00:45,880 That works, but then all of a sudden I decide to add hello to one. 8 00:00:47,170 --> 00:00:50,800 And yet again, we see that red this time a type ER. 9 00:00:55,460 --> 00:00:59,960 That crashes our program like this is called an exception. 10 00:01:01,450 --> 00:01:02,680 And Python. 11 00:01:03,960 --> 00:01:10,710 Raises these exceptions whenever the interpreter says, hey, I have no idea what you're doing, something's 12 00:01:10,710 --> 00:01:15,180 wrong, I don't know what I'm doing anymore, I'm going to stop whatever I'm doing and I'm going to 13 00:01:15,180 --> 00:01:16,560 give you an output. 14 00:01:18,400 --> 00:01:25,780 This is bad because right now we're just playing, but if we're Google and people depend on us to maybe 15 00:01:25,780 --> 00:01:32,530 check their email, to watch YouTube videos, if our programs have exceptions and they stop working 16 00:01:32,530 --> 00:01:34,540 and they eera out, that's bad. 17 00:01:35,780 --> 00:01:37,950 That could cost companies millions. 18 00:01:38,600 --> 00:01:42,920 So how can we handle these exceptions that crash our program? 19 00:01:43,700 --> 00:01:47,160 Well, we need something called air handling. 20 00:01:47,660 --> 00:01:54,070 It's a natural part of being a programmer and a great programmer is able to handle errors in their program. 21 00:01:55,100 --> 00:02:01,580 Because instead of letting our programs just air, we're able to handle them so that if we get something 22 00:02:01,580 --> 00:02:07,070 like a type like this, we can actually handle that air within our programs. 23 00:02:07,430 --> 00:02:09,630 And in this section, we're going to learn all about that. 24 00:02:10,430 --> 00:02:18,260 But the key takeaway is that air handling allows us to let the Python script continue running, even 25 00:02:18,260 --> 00:02:19,610 if there are errors. 26 00:02:20,150 --> 00:02:21,360 Sounds pretty interesting, right? 27 00:02:22,130 --> 00:02:26,830 Well, before we get into that, let's just talk about some of the errors that exist in Python. 28 00:02:27,680 --> 00:02:29,090 We've already seen type error. 29 00:02:29,360 --> 00:02:35,150 Where are we trying to do something between two different data types that are incompatible, such as 30 00:02:35,150 --> 00:02:40,390 adding ENT and string and these built-In exceptions in Python? 31 00:02:41,000 --> 00:02:42,260 There's lots of them. 32 00:02:42,380 --> 00:02:44,530 And I'll link to this resource so you can take a look. 33 00:02:44,690 --> 00:02:50,840 But if I scroll through here, you see attribute, error and of failure, we keep going. 34 00:02:50,840 --> 00:02:52,160 We have a name error. 35 00:02:52,190 --> 00:02:54,530 We have so many error reference. 36 00:02:54,530 --> 00:02:56,210 There's just a ton. 37 00:02:57,850 --> 00:03:00,250 Let me just go through some of them that are quite common. 38 00:03:01,700 --> 00:03:06,500 One is a syntax error if I do def and let's say. 39 00:03:07,970 --> 00:03:08,610 Whoo hoo! 40 00:03:09,500 --> 00:03:13,340 And I forget a semicolon here and I run this. 41 00:03:14,930 --> 00:03:19,490 I get a syntax error, it's not a standard python syntax, I have to fix my error. 42 00:03:20,760 --> 00:03:29,760 I can also get something called a Namir if let's say I use one plus name like this and I click. 43 00:03:32,080 --> 00:03:38,800 Name like this, you can see the red underline here, I get undefined name so that if I run this code. 44 00:03:42,070 --> 00:03:44,620 I'll get a nightmare name is not defined. 45 00:03:45,940 --> 00:03:47,020 We've seen type ER. 46 00:03:47,800 --> 00:03:53,860 But what about this, what have we have a list, let's say Elai. 47 00:03:55,340 --> 00:04:02,090 One, two, three, and I access Elai, let's say an index of five if I run this. 48 00:04:03,320 --> 00:04:06,560 I get an index error list index is out of range. 49 00:04:07,910 --> 00:04:13,070 What if we had a dictionary, so let's say we're creating a dictionary here. 50 00:04:14,380 --> 00:04:15,280 We'll say a. 51 00:04:16,570 --> 00:04:19,910 Cause one and, you know, I will just leave it at that. 52 00:04:20,440 --> 00:04:22,780 So this will be a dictionary. 53 00:04:24,210 --> 00:04:29,400 If I do dictionary here and just try to access something that doesn't exist and I click run. 54 00:04:31,220 --> 00:04:32,420 I get a key error. 55 00:04:33,360 --> 00:04:37,260 Because I'm accessing a key that doesn't exist. 56 00:04:38,390 --> 00:04:46,130 We can also have, let's say, five divided by zero, which should give me a zero division error because 57 00:04:46,130 --> 00:04:49,400 I'm trying to divide by zero and well, you just can't do that. 58 00:04:50,220 --> 00:04:54,450 So there's lots of errors or built-In exceptions in Python. 59 00:04:55,040 --> 00:04:57,130 Now, you don't have to memorize any of these. 60 00:04:57,410 --> 00:05:01,180 Most of the time they'll come up in your program and you'll know what they mean. 61 00:05:01,430 --> 00:05:04,130 But you can always use this as a reference to read them. 62 00:05:05,150 --> 00:05:09,320 The question you should be asking yourself is, how do we avoid these? 63 00:05:10,860 --> 00:05:18,750 Because most programs aren't just us typing information, usually programs interact with the outside 64 00:05:18,750 --> 00:05:19,090 world. 65 00:05:19,200 --> 00:05:25,800 Right when I log into my email, I let a user use their email and password. 66 00:05:26,490 --> 00:05:29,810 When I play a game, I let a user control a character. 67 00:05:30,480 --> 00:05:36,720 So programs are constantly interacting with the outside world and they have to make sure that whatever 68 00:05:36,750 --> 00:05:41,010 input they get is the right type that they're expecting. 69 00:05:41,370 --> 00:05:48,450 Because if they receive something that they didn't expect like number and this number just happens to 70 00:05:48,450 --> 00:05:51,390 be, let's say, zero, that we give it. 71 00:05:53,270 --> 00:06:01,550 Well, this user broke our program, so we need to handle these situations where a user might give something 72 00:06:01,550 --> 00:06:02,930 bad that we wouldn't want. 73 00:06:03,620 --> 00:06:07,880 In the next video, we're going to explore how we can handle our errors.