1 00:00:12,320 --> 00:00:18,740 Welcome, everybody, very happy that you're here with me and we're going to talk about the second part 2 00:00:18,740 --> 00:00:27,200 of exceptions and error handling so and the previous video, we have seen several examples of popular 3 00:00:27,440 --> 00:00:29,960 errors that can be raised in Python language. 4 00:00:30,260 --> 00:00:35,260 And we have seen like what the code that causes these errors to be raised. 5 00:00:35,270 --> 00:00:42,710 And also we have seen this sibling error which was raised in the terminal below in charm. 6 00:00:42,980 --> 00:00:44,860 We have seen the name of the error. 7 00:00:44,870 --> 00:00:48,760 And we also see that it points out to exactly where the error happened. 8 00:00:49,610 --> 00:00:57,860 So that was kind of the negative part that our code basically can fail and it can stop being executed 9 00:00:57,860 --> 00:01:01,930 because we also mentioned once by chance encounters an error. 10 00:01:01,940 --> 00:01:03,460 It basically stops from running. 11 00:01:03,800 --> 00:01:10,280 So every code that you are going to have below the line that caused the error would not be executed. 12 00:01:10,290 --> 00:01:15,350 So this is kind of like the pessimist side of the topic of errors. 13 00:01:15,710 --> 00:01:21,980 But today we're going to talk about the positive side of exceptions because exceptions can be managed 14 00:01:21,980 --> 00:01:26,570 and basically we can have full control overall code and prevent it from failing. 15 00:01:27,710 --> 00:01:35,270 So in Python, we have this tri except statement, which will give us the full control once we have 16 00:01:35,270 --> 00:01:36,680 an error in execution. 17 00:01:38,660 --> 00:01:46,640 And this part basically tells us that, as we just mentioned, that the immediately the execution stops, 18 00:01:46,820 --> 00:01:54,960 once it encounters an error, but we can't have a full control of our code and stop it from failing. 19 00:01:54,980 --> 00:01:55,430 All right. 20 00:01:56,030 --> 00:01:58,930 And we're going to use the tricep statement to do it. 21 00:01:59,540 --> 00:02:02,540 So we're going to discuss about the code flow. 22 00:02:03,110 --> 00:02:10,720 Basically, what are the things that are happening behind the scenes once we use this dry except statement? 23 00:02:10,970 --> 00:02:16,400 So we're going to see what happens with the code flow and also going to have a quick implementation 24 00:02:16,400 --> 00:02:22,520 example where we see exactly what happens if an error is encountered in the execution. 25 00:02:23,240 --> 00:02:23,770 All right. 26 00:02:24,290 --> 00:02:32,000 So you can see here how dry except looks like you basically going to have a try keyword in some code 27 00:02:32,000 --> 00:02:35,800 inside it, same as we had in four loops and if statements. 28 00:02:35,820 --> 00:02:39,680 OK, so here we have dry statement and we have certain code inside it. 29 00:02:39,950 --> 00:02:45,320 And right after you have a sibling, except and you also going to have some code inside this one as 30 00:02:45,320 --> 00:02:45,620 well. 31 00:02:46,460 --> 00:02:50,710 It's important to mention that you have to use these both together. 32 00:02:50,720 --> 00:02:56,930 There are siblings and if you only write dry and not mentioning except in your code, you are going 33 00:02:56,930 --> 00:02:57,700 to have an error. 34 00:02:57,710 --> 00:03:00,460 So always use both of them together. 35 00:03:00,990 --> 00:03:03,230 Now, let's talk about the dynamics between these two. 36 00:03:04,960 --> 00:03:12,040 So let's start from here once we run a file that has dry except statement, quote, execution will first 37 00:03:12,040 --> 00:03:13,500 run what's inside dry. 38 00:03:13,900 --> 00:03:16,810 So first of all, the code will go inside the dry. 39 00:03:16,990 --> 00:03:22,600 And if you have any variables or print print statements in here, it will run it. 40 00:03:22,600 --> 00:03:24,880 Whatever any code you right here, it will run it. 41 00:03:25,900 --> 00:03:27,310 And then you have two options. 42 00:03:27,850 --> 00:03:35,230 If no errors would be raised in my statement, the code will completely one, sorry, the call will 43 00:03:35,230 --> 00:03:41,260 complete the execution within try and we will ignore the part completely. 44 00:03:41,890 --> 00:03:42,380 OK. 45 00:03:42,640 --> 00:03:47,170 So if this part in here and so dry, you do not have any errors. 46 00:03:47,440 --> 00:03:53,260 So it will complete the execution inside dry and will ignore the accept part completely. 47 00:03:53,530 --> 00:04:04,510 And then whatever is after whatever appears, whatever code appears after dry would also run. 48 00:04:04,690 --> 00:04:12,370 OK, this is like the usual stuff, you know, like once the cold inside dry will be executed, if you 49 00:04:12,370 --> 00:04:15,490 have any code here below, it will also be executed. 50 00:04:15,520 --> 00:04:22,000 OK, so not just going to stop and dry, except like the normal behavior of code. 51 00:04:22,660 --> 00:04:25,420 If you don't have any errors, it will just simply run. 52 00:04:25,690 --> 00:04:26,140 All right. 53 00:04:27,400 --> 00:04:37,330 But if you have an air or reste inside this dry statement right here, the cold will not fail or stop. 54 00:04:37,330 --> 00:04:43,090 And this is the important point we need to stop and discuss about, because in the previous video we 55 00:04:43,090 --> 00:04:48,820 have seen, we have created some code which cost us an exception. 56 00:04:48,940 --> 00:04:54,160 And then the execution basically stopped immediately and it did not continue to run. 57 00:04:54,160 --> 00:04:56,170 Just wrote to us in the terminal below. 58 00:04:56,350 --> 00:04:58,900 We have an exception and the code stopped from running. 59 00:04:59,230 --> 00:05:07,990 But in this case right here, the code that inside Trie had an exception and it did not stop the execution 60 00:05:07,990 --> 00:05:09,610 basically jumped immediately. 61 00:05:10,000 --> 00:05:14,500 And it's starting to execute whatever is inside the except. 62 00:05:14,900 --> 00:05:20,200 OK, so the execution will instantly move to executing of what's inside. 63 00:05:20,200 --> 00:05:23,230 Except, OK, so this is the whole deal. 64 00:05:23,530 --> 00:05:29,950 This is how we get full control when having an error during execution, because if having some problematic 65 00:05:29,950 --> 00:05:37,990 code, we should, you should think about putting the problem code or dangerous code inside dry and 66 00:05:37,990 --> 00:05:45,400 then if it gets an error it will immediately go to accept and it will continue to execute our program. 67 00:05:47,290 --> 00:05:56,380 So that part was regardless, regardless of implementation, it basically was a simple code flow explanation. 68 00:05:56,680 --> 00:06:03,280 What happens inside here and now we're going to see an implementation error of what really happens if 69 00:06:03,280 --> 00:06:06,010 we're encountering an error in live code. 70 00:06:06,700 --> 00:06:07,150 All right. 71 00:06:07,810 --> 00:06:15,850 So this example right here, you can see declaration of try and accept using these two keywords and 72 00:06:15,850 --> 00:06:20,260 then you have some indention inside attrite, same as you have inside. 73 00:06:20,260 --> 00:06:23,230 If statements are for loops and just need to have some indention. 74 00:06:23,740 --> 00:06:27,670 And right here we having this if age is smaller than twenty. 75 00:06:28,180 --> 00:06:28,570 Yeah. 76 00:06:28,570 --> 00:06:35,020 The if statement while we see that we did not declare the edge of a variable before, OK, we're just 77 00:06:35,020 --> 00:06:40,570 using age smaller than twenty, but we did not put any value to age. 78 00:06:40,570 --> 00:06:43,300 So basically this causes a syntax error. 79 00:06:43,840 --> 00:06:47,770 OK, this execution of this card will cause a syntax error. 80 00:06:47,980 --> 00:06:53,230 And this is the first example that we have seen and the list of errors in the previous video. 81 00:06:54,490 --> 00:07:00,840 But in the previous video, we did not have dry, except we just had this if some number smaller than 82 00:07:00,850 --> 00:07:01,600 some value. 83 00:07:01,780 --> 00:07:07,690 We had an exception because we did not declare the variable and then the code stopped from running. 84 00:07:07,930 --> 00:07:11,050 But this time we're having this variable inside. 85 00:07:11,050 --> 00:07:17,560 Try and let's try to apply the rules that we have went over here to this example. 86 00:07:17,830 --> 00:07:25,390 So one basically will happen when the code reaches this if here this print would not be executed. 87 00:07:25,720 --> 00:07:33,760 As we mentioned that when having this exception in this line right here, it immediately jumps to the 88 00:07:33,760 --> 00:07:41,720 accept part and the code will execute whatever inside the except ignoring the code that comes after 89 00:07:41,780 --> 00:07:42,880 the exception right here. 90 00:07:42,880 --> 00:07:43,300 And try. 91 00:07:45,070 --> 00:07:52,360 And this exception perhaps here, age variable was not assigned with any value setting value. 92 00:07:52,390 --> 00:07:52,960 Three now. 93 00:07:53,020 --> 00:07:54,540 OK, I'm doing whatever I want here. 94 00:07:54,550 --> 00:07:56,100 I have full control over the code. 95 00:07:56,410 --> 00:07:59,980 Basically, I recognized that I'm going to have a syntax error here. 96 00:08:00,190 --> 00:08:05,400 So I immediately know the reasons I wrote here to the end user. 97 00:08:05,410 --> 00:08:10,510 Whoever runs the program, each variable was not assigned with any value setting value three now. 98 00:08:11,110 --> 00:08:17,530 And once I encountered this error, basically here I'm giving a value off three to the age variable. 99 00:08:17,680 --> 00:08:23,170 And then I'm asking again, if age is smaller than 20 and if it's true, then this person is very young. 100 00:08:24,250 --> 00:08:27,670 So we basically redirect the code. 101 00:08:27,880 --> 00:08:32,530 Once an error is raised by China will continue the run from what's inside. 102 00:08:32,530 --> 00:08:39,040 Except I think this covers the dry accept and error handling pretty good. 103 00:08:39,820 --> 00:08:41,290 The topics we have covered. 104 00:08:41,290 --> 00:08:48,400 We have seen lots of examples of exceptions and also now we are familiar of how controlling the code 105 00:08:48,400 --> 00:08:54,010 and stopping from the exceptions of failing our execution. 106 00:08:54,190 --> 00:08:54,670 All right. 107 00:08:55,180 --> 00:09:00,280 Let's see the following, the actual regarding our an assignment, the homework assignment regarding 108 00:09:00,280 --> 00:09:01,360 off tricep. 109 00:09:01,690 --> 00:09:08,560 And there I will give you a preprepared bulk of code that would raise a certain exception. 110 00:09:08,680 --> 00:09:12,970 And I would like you to use tricep to overcome from that cold from failing. 111 00:09:13,360 --> 00:09:14,980 I will see you in the next lecture. 112 00:09:15,190 --> 00:09:15,670 Thank you.