1 00:00:12,040 --> 00:00:14,450 Hi, everybody, welcome to the lecture. 2 00:00:15,190 --> 00:00:20,560 We're going to start off with a new topic and we're going to discuss exceptions and errors in the code 3 00:00:21,460 --> 00:00:24,940 once we have completed the challenging part of OLP. 4 00:00:25,300 --> 00:00:29,810 Now it's time to know how to handle exceptions and errors in our code. 5 00:00:30,160 --> 00:00:35,560 In a perfect world, a programmer would create a piece of code that will run perfectly and would not 6 00:00:35,560 --> 00:00:37,390 have any fair points or errors. 7 00:00:37,780 --> 00:00:44,860 But in the real world, in real programming projects, as they grow, they have more and more potential 8 00:00:44,860 --> 00:00:46,990 to have exceptions and errors in them. 9 00:00:47,890 --> 00:00:55,270 And we better know we better be prepared to handle them and not leave it to luck and hope that we would 10 00:00:55,270 --> 00:00:56,640 not have these errors. 11 00:00:57,490 --> 00:01:05,380 We would need to have a sort of prediction ability to know how to catch these errors or even fix them 12 00:01:05,380 --> 00:01:07,300 in the code once we encounter them. 13 00:01:09,570 --> 00:01:17,310 In computer science are once the program encounters an error, it is called an exception. 14 00:01:18,510 --> 00:01:18,960 All right. 15 00:01:18,960 --> 00:01:23,190 So let's go with the slides here and see the following examples. 16 00:01:23,560 --> 00:01:29,400 An exception would happen once a program is executed and it would encounter a wrong piece of code. 17 00:01:30,120 --> 00:01:37,080 By the way, it can come in many ways and shapes and we have many types of errors and we will go over 18 00:01:37,320 --> 00:01:39,660 the common ones in a few minutes. 19 00:01:41,070 --> 00:01:49,620 So when the code execution encounters the wrong piece of code, it will cause the code to fail at that 20 00:01:49,620 --> 00:01:50,100 point. 21 00:01:50,130 --> 00:01:56,790 Python does not know what to do with the defective code, and it basically terminates the execution 22 00:01:56,790 --> 00:01:57,540 on the spot. 23 00:01:59,140 --> 00:02:07,750 Biden also knows exactly what type of exception is thrown once it happens, and then we can apply certain 24 00:02:07,750 --> 00:02:17,350 actions to either get a flow of code, to do something, to do some commands or either to fix the code. 25 00:02:19,210 --> 00:02:27,820 So how do we know that our code is failing and that we had an exception, so once we run a certain Python 26 00:02:27,820 --> 00:02:32,340 file and the code will encounter an exception, the run will stop. 27 00:02:32,530 --> 00:02:41,710 So by the look of our picture, if we're looking at our code stopped in the middle, it's probably because 28 00:02:41,710 --> 00:02:45,010 we had an exception during the run. 29 00:02:45,250 --> 00:02:52,990 If the code flow will encounter a badly written code, it will cause the run to fail and the error would 30 00:02:52,990 --> 00:02:56,600 be displayed at the terminal log off by chance in red color. 31 00:02:56,950 --> 00:03:06,300 So once we are executing a code and we will encounter a certain exception, it will be printed for us 32 00:03:06,310 --> 00:03:08,650 so we could read and debug it. 33 00:03:08,650 --> 00:03:16,180 And to understand what that means, the terminal would also give us indication and what class method 34 00:03:16,180 --> 00:03:23,890 or module the exception happened and also would point us to the exact line in the code so we could look 35 00:03:23,890 --> 00:03:26,320 at that problematic line and fix it. 36 00:03:28,140 --> 00:03:36,120 So once we are familiar with, like, what are the signs that we had an exception and where can we find 37 00:03:36,120 --> 00:03:40,890 and read that exception, let's see a list of exceptions. 38 00:03:41,070 --> 00:03:45,360 And I picked up the popular ones because there are many of them. 39 00:03:45,360 --> 00:03:52,560 And covering all the exceptions is irrelevant, because once you get the idea of what is an exception, 40 00:03:52,860 --> 00:03:59,420 even if you will encounter is some exception that you're not familiar with, you could go to Google, 41 00:03:59,430 --> 00:04:01,100 read about it and learn more. 42 00:04:01,800 --> 00:04:04,320 But we're going to talk about the common ones. 43 00:04:04,440 --> 00:04:08,240 I picked that up for you and you will see some examples as well. 44 00:04:08,670 --> 00:04:10,560 So let's begin first. 45 00:04:10,560 --> 00:04:13,200 One is syntax error or parsing error. 46 00:04:13,680 --> 00:04:20,760 This error will because once the code will encounter a programmatic logic of failure or once we will 47 00:04:20,760 --> 00:04:30,390 not fall to Python syntax rules for the example of that syntax error or parsing like the come as a power 48 00:04:31,020 --> 00:04:31,890 you can see here. 49 00:04:31,890 --> 00:04:35,340 And if statement if A is below, then three. 50 00:04:35,670 --> 00:04:41,060 But it was caused because we did not put any value in a OK. 51 00:04:41,130 --> 00:04:44,940 We did not declare the variable before using it. 52 00:04:45,210 --> 00:04:48,810 So the terminal would give us this exception right here. 53 00:04:49,050 --> 00:04:56,910 And we could see here that it happened in this file, Eurhythmics exception UI. 54 00:04:58,200 --> 00:05:01,700 And you can see here that if you would click on it. 55 00:05:02,070 --> 00:05:06,990 So first of all, we know the name of the file and we have also indication what line happened. 56 00:05:06,990 --> 00:05:07,710 Line one. 57 00:05:09,130 --> 00:05:15,420 And also, we have a small error pointing up here, pointing us to where exactly the exception happened. 58 00:05:15,430 --> 00:05:22,270 So it's basically here in the statement of values and also we have the exception itself, syntax error. 59 00:05:30,000 --> 00:05:34,390 Right here, and we need to fix it, so this is for syntax error. 60 00:05:34,890 --> 00:05:39,220 Let's see the second example of zero division error. 61 00:05:39,570 --> 00:05:41,390 OK, the name speaks for itself. 62 00:05:41,700 --> 00:05:48,390 Basically, we are getting this exception once we're doing an illegal mathematical action, which is 63 00:05:48,570 --> 00:05:50,800 dividing a certain number by zero. 64 00:05:51,090 --> 00:05:56,180 So in the real world, regardless of programming, you cannot divide anything by zero. 65 00:05:56,370 --> 00:05:58,010 It's an illegal math action. 66 00:05:58,260 --> 00:06:03,510 So Python will tell us that zero division error division by zero. 67 00:06:03,540 --> 00:06:05,690 OK, so this is also an exception. 68 00:06:05,970 --> 00:06:09,240 This will cause our code stop running. 69 00:06:09,430 --> 00:06:15,000 So if we have these two lines and then we have a hundred more lines, the code will stop here. 70 00:06:17,290 --> 00:06:25,090 Third one in line is tie power, and it will occur once using a wrong type of object and it can appear 71 00:06:25,090 --> 00:06:27,670 in many scenarios, I just gave you a one here. 72 00:06:28,540 --> 00:06:32,050 We already encountered this scenario many times. 73 00:06:32,050 --> 00:06:39,190 In the course we have here, X equals five, and then we're trying to print a string together with an 74 00:06:39,190 --> 00:06:39,780 integer. 75 00:06:39,940 --> 00:06:49,960 And this would cause us the type error you can see here can only concatenate string nuttiness to string. 76 00:06:51,040 --> 00:06:56,380 And also pointing out here the line and also pointing out the name of the file. 77 00:06:58,360 --> 00:07:00,290 So how can we fix it? 78 00:07:00,630 --> 00:07:02,290 We already did it many times. 79 00:07:02,530 --> 00:07:06,510 We should fix it by using casting in that order. 80 00:07:09,400 --> 00:07:17,200 All right, fourth in line, name error name is caused when a variable cannot be found in local or global 81 00:07:17,200 --> 00:07:17,710 scope. 82 00:07:17,980 --> 00:07:25,900 So here we have a math accident, X plus five, but we never created X, so Python does not know what 83 00:07:25,900 --> 00:07:27,330 to do with the statement. 84 00:07:27,340 --> 00:07:32,860 He's stuck, he's confused, and he's going to fail the execution once we do that. 85 00:07:33,130 --> 00:07:39,790 So we, first of all, need to create a variable and then to put value to it. 86 00:07:39,790 --> 00:07:42,130 And only then we can use in the math section. 87 00:07:43,420 --> 00:07:51,820 So we never created the X here and we have here name error, name X is not defined also line number 88 00:07:51,820 --> 00:07:52,930 and filename as well. 89 00:07:54,550 --> 00:08:02,220 Index error is caused when reaching out for a sell off collection by index and that index does not exist. 90 00:08:02,650 --> 00:08:06,070 OK, so we have a list here that at least have three. 91 00:08:06,070 --> 00:08:07,570 Index three is the last one. 92 00:08:07,750 --> 00:08:12,040 And here we try out to pull index six, which is. 93 00:08:13,090 --> 00:08:17,920 Does not exist, basically, so we have index our list, index out of range. 94 00:08:21,970 --> 00:08:29,830 All right, so we have indention error, as you already know, Python has indention rules and these 95 00:08:29,830 --> 00:08:36,720 inventions, they basically give us the hierarchy of the code and the flow of code. 96 00:08:36,730 --> 00:08:42,700 And we need to keep you keep implementing according to indention rules. 97 00:08:43,160 --> 00:08:49,420 So in this case, you can see here that equals force equals four does not have an intention, but the 98 00:08:49,420 --> 00:08:52,650 print afterwards has indention, and that's wrong. 99 00:08:52,660 --> 00:08:58,870 This will cause our code to fail and raise the exception in the error least index out of range. 100 00:09:00,760 --> 00:09:09,010 Kierra wants to try to call out a dictionary cell with a wrong or nonexistent key, so we have the key 101 00:09:09,220 --> 00:09:15,700 here, A, B, C, the value one, and then we try to reach out by using the key Z, and that key does 102 00:09:15,700 --> 00:09:16,360 not exist. 103 00:09:16,510 --> 00:09:18,880 So we're going to get an indention error. 104 00:09:19,000 --> 00:09:28,420 Unexpecting indent eight one in line value error will be caused by using a wrong value, a value that 105 00:09:28,420 --> 00:09:30,140 cannot be used in a certain command. 106 00:09:30,400 --> 00:09:33,370 So we're trying to cast the string dog. 107 00:09:33,710 --> 00:09:40,060 Dog is obviously letters and we're trying to to make it to convert it to Cassity to an end. 108 00:09:40,690 --> 00:09:42,040 This cannot be possible. 109 00:09:42,370 --> 00:09:46,630 And we're going to have this exception value for dog. 110 00:09:49,110 --> 00:09:56,100 OK, so these items were the common exceptions that you're going to encounter in your code, basically 111 00:09:56,790 --> 00:10:01,700 you're going to have many more exceptions in general in Python. 112 00:10:01,950 --> 00:10:07,770 And once you encountered the you just Google it and you find the exact explanation why the exception 113 00:10:07,770 --> 00:10:08,350 happened. 114 00:10:08,370 --> 00:10:14,660 And once you know why, you can either fix it or to overcome it in some different techniques. 115 00:10:15,030 --> 00:10:15,510 Thank you. 116 00:10:15,510 --> 00:10:16,920 And I will see you in the next lecture.