1 00:00:00,009 --> 00:00:02,710 As you continue to progress in your journey as 2 00:00:02,720 --> 00:00:06,190 a Python programmer or as a programmer in general, 3 00:00:06,469 --> 00:00:10,539 there is a habit that I want you to adopt and this habit 4 00:00:10,899 --> 00:00:14,850 is gonna involve writing programs or designing programs 5 00:00:15,130 --> 00:00:18,649 that are able to handle potential errors 6 00:00:18,760 --> 00:00:20,909 in a graceful manner. 7 00:00:21,299 --> 00:00:23,280 So the thing is most programmers out there, 8 00:00:23,290 --> 00:00:27,190 they create all these kinds of great programs that can do this to do that. 9 00:00:27,409 --> 00:00:29,309 However, if something goes wrong, 10 00:00:29,629 --> 00:00:33,869 maybe the user of the program added some type of an input 11 00:00:33,959 --> 00:00:37,659 or maybe they used numbers instead of letters or something like that, 12 00:00:37,669 --> 00:00:38,009 you know, 13 00:00:38,790 --> 00:00:43,040 many times these programs end up crashing because the programmer 14 00:00:43,619 --> 00:00:44,810 hasn't 15 00:00:45,060 --> 00:00:47,020 designed a program in such a way 16 00:00:47,130 --> 00:00:49,610 that if such potential errors do occur, 17 00:00:49,819 --> 00:00:52,380 the program knows how to handle it. 18 00:00:52,869 --> 00:00:56,389 We have talked about handling errors briefly when we talked about loops, 19 00:00:56,400 --> 00:00:58,849 but now we're going to go a bit more in depth 20 00:00:59,419 --> 00:01:00,470 on my screen right now. 21 00:01:00,479 --> 00:01:05,769 I have a very simple program that reads the file, the example dot CXT. 22 00:01:06,230 --> 00:01:10,269 But note however, that this program, the short block of code, 23 00:01:10,660 --> 00:01:16,639 it relies on this file example dot TXT actually existing 24 00:01:17,610 --> 00:01:18,800 what if 25 00:01:19,419 --> 00:01:23,330 I maybe made a mistake or the use of the program, 26 00:01:23,660 --> 00:01:25,900 the user of the program, you made a mistake. 27 00:01:26,250 --> 00:01:31,879 And instead of example dot txt, we have example dot txt. 28 00:01:32,160 --> 00:01:36,290 Now this file doesn't exist. It's not in my directory. 29 00:01:36,910 --> 00:01:40,849 If I run the program, now, you can see we have an arrow. 30 00:01:41,599 --> 00:01:45,989 Python is telling us that, hey, sorry, no such file or directory file, 31 00:01:46,010 --> 00:01:46,830 not found error. 32 00:01:47,449 --> 00:01:49,169 This doesn't look good, right? So 33 00:01:49,900 --> 00:01:55,110 we want to write our programs in such a way that if such an error does occur, 34 00:01:56,199 --> 00:02:00,099 it's going to handle it in a very graceful and professional manner. So 35 00:02:00,970 --> 00:02:04,809 what we do right now is let me go back in here and add example 36 00:02:05,199 --> 00:02:06,489 dot txt. OK? 37 00:02:06,910 --> 00:02:07,470 So 38 00:02:07,580 --> 00:02:10,589 we're gonna use the try except 39 00:02:11,029 --> 00:02:11,820 block. 40 00:02:11,960 --> 00:02:14,240 We've talked about this when we talked about loops, but 41 00:02:14,429 --> 00:02:15,910 let's talk about it again. So 42 00:02:16,190 --> 00:02:17,720 what you want to do is 43 00:02:18,020 --> 00:02:23,220 in the block of code where there's a potential for the error to occur 44 00:02:23,380 --> 00:02:25,160 such as you know, over here, 45 00:02:25,600 --> 00:02:28,259 one thing you want to do is you want to start off by saying try, 46 00:02:29,020 --> 00:02:33,229 try and now make sure it's indented properly because 47 00:02:33,850 --> 00:02:35,789 the code after try should be under it. 48 00:02:36,139 --> 00:02:37,960 So now we're saying, OK, 49 00:02:38,130 --> 00:02:42,919 try to open this file example dot txt. 50 00:02:43,559 --> 00:02:45,220 And if it exists and all that, 51 00:02:45,360 --> 00:02:48,270 you can print out the contents of the file, 52 00:02:48,410 --> 00:02:49,110 OK? 53 00:02:50,199 --> 00:02:50,899 However, 54 00:02:51,949 --> 00:02:57,289 what if the file does not exist now, we can say accept 55 00:02:58,720 --> 00:03:01,059 and then under accept, 56 00:03:01,419 --> 00:03:03,059 we can say print 57 00:03:03,770 --> 00:03:07,789 and then we can say something like uh file not found. 58 00:03:08,289 --> 00:03:08,910 OK, 59 00:03:09,240 --> 00:03:11,110 let's add a colon where 60 00:03:11,320 --> 00:03:12,029 accept 61 00:03:12,320 --> 00:03:14,869 and there it is. So basically we're saying, hey, 62 00:03:15,149 --> 00:03:18,190 try to run this block of code 63 00:03:18,899 --> 00:03:21,160 and if for one reason or the other, 64 00:03:21,869 --> 00:03:25,809 the file isn't found and simply print out file, not found. So 65 00:03:26,199 --> 00:03:27,649 if I run the program right now, 66 00:03:27,660 --> 00:03:30,089 everything works properly because the file does exist. 67 00:03:30,100 --> 00:03:30,729 However, 68 00:03:31,009 --> 00:03:32,729 if I change the name of the file 69 00:03:33,139 --> 00:03:34,360 and I run the program, 70 00:03:34,580 --> 00:03:39,899 you can see very professionally. It simply says file not found. 71 00:03:40,009 --> 00:03:42,970 And that's because we're using the try except block 72 00:03:44,339 --> 00:03:47,160 while this works well 73 00:03:48,559 --> 00:03:49,820 to go deeper. 74 00:03:50,000 --> 00:03:52,300 I want you to start using 75 00:03:52,580 --> 00:03:58,539 what we call the common file related exceptions. There's quite a variety of them 76 00:03:58,750 --> 00:04:01,529 and Python has specific exceptions 77 00:04:01,660 --> 00:04:02,399 for 78 00:04:02,759 --> 00:04:05,119 many of the common file related errors. 79 00:04:05,130 --> 00:04:08,809 The the first one and the most common one is the file, 80 00:04:08,820 --> 00:04:12,860 not found error just as we have over here. 81 00:04:13,500 --> 00:04:14,089 So 82 00:04:14,479 --> 00:04:18,940 all you need to do is instead of just simply saying, try except 83 00:04:19,079 --> 00:04:20,809 where you have the accept, 84 00:04:21,298 --> 00:04:23,559 you can type in file, 85 00:04:24,269 --> 00:04:26,799 not found error. 86 00:04:27,869 --> 00:04:29,489 This right here, 87 00:04:29,910 --> 00:04:33,339 we're telling Python to specifically 88 00:04:33,529 --> 00:04:39,839 check to see if the error is caused by the fact that the file was not found. 89 00:04:40,040 --> 00:04:40,570 So 90 00:04:40,720 --> 00:04:44,500 if I run my program again, right now, you can see it still works perfectly fine. 91 00:04:44,510 --> 00:04:45,489 However, 92 00:04:45,619 --> 00:04:49,049 this is a much cleaner and a more efficient way 93 00:04:49,269 --> 00:04:51,850 of handling the error. Because over here 94 00:04:52,084 --> 00:04:55,204 we're specifically checking to see if the 95 00:04:55,214 --> 00:04:57,984 error occurred because the file doesn't exist 96 00:04:58,265 --> 00:05:03,864 the try except block on its own handles all errors, 97 00:05:04,035 --> 00:05:05,945 whether the file wasn't found, 98 00:05:06,065 --> 00:05:06,725 whether it's, 99 00:05:06,734 --> 00:05:09,345 it's a permission error or maybe it's a directory 100 00:05:09,355 --> 00:05:11,674 error or maybe it's an open system error. 101 00:05:11,684 --> 00:05:13,845 It will handle everything. So 102 00:05:14,309 --> 00:05:17,809 yes, it will work quite a lot. It will handle the error. 103 00:05:18,049 --> 00:05:20,769 But again, a cleaner way will be to specify 104 00:05:20,970 --> 00:05:24,570 the kind of error that you are actually expecting. So 105 00:05:25,140 --> 00:05:27,290 we have the file, not found error. 106 00:05:27,709 --> 00:05:31,309 We also have the permission error. 107 00:05:31,839 --> 00:05:33,529 Maybe for example, 108 00:05:33,700 --> 00:05:34,609 the 109 00:05:34,739 --> 00:05:40,290 uh the file in question is can only be accessed by an admin. 110 00:05:40,769 --> 00:05:43,230 And now it's trying to be accessed by a regular user. 111 00:05:43,239 --> 00:05:45,829 Obviously, you're going to have the error saying, hey, 112 00:05:46,339 --> 00:05:48,519 so you don't have permission. So in 113 00:05:48,730 --> 00:05:51,940 this kind of scenario, all we'll simply do is we'll just say, 114 00:05:52,619 --> 00:05:58,279 you know, sorry you do, you do not have permission 115 00:05:59,019 --> 00:06:00,959 to view this file, 116 00:06:01,070 --> 00:06:03,410 you know, and that's all we would need to do. 117 00:06:03,540 --> 00:06:05,459 So if you're expecting a permission error, 118 00:06:05,790 --> 00:06:10,100 you want to go with the accept permission error, block of code. 119 00:06:10,290 --> 00:06:14,339 We also have the is a directory error. 120 00:06:14,559 --> 00:06:18,760 Maybe the user was trying to open up a directory that doesn't exist again, 121 00:06:19,529 --> 00:06:20,589 simply come in here 122 00:06:21,380 --> 00:06:22,440 and then you type in 123 00:06:22,619 --> 00:06:23,899 is a 124 00:06:24,089 --> 00:06:26,920 directory error and there it is. And now from here, 125 00:06:27,250 --> 00:06:31,250 we can specify the exact print message. So in 126 00:06:31,440 --> 00:06:33,459 this case, it will be something like sorry, 127 00:06:33,600 --> 00:06:37,859 you don't have permission to access this directory or sorry. 128 00:06:37,869 --> 00:06:41,440 This directory doesn't exist, you know, something like that. And then the last one 129 00:06:41,739 --> 00:06:44,420 is going to be the operating system error. 130 00:06:44,429 --> 00:06:48,260 So this is typically the general Exception for other file 131 00:06:48,380 --> 00:06:49,399 related errors. 132 00:06:49,470 --> 00:06:53,720 Maybe the disk is full, maybe it's an input output issue, something like that. 133 00:06:53,859 --> 00:06:54,440 So 134 00:06:54,950 --> 00:06:57,200 all you need to do right now is just say accept 135 00:06:58,089 --> 00:06:58,709 and then 136 00:07:00,010 --> 00:07:01,670 OS 137 00:07:02,160 --> 00:07:02,470 arrow 138 00:07:03,380 --> 00:07:05,850 and then you can just type in something like sorry, 139 00:07:06,269 --> 00:07:09,820 an error has occurred or an open system error has occurred, something like that. 140 00:07:09,829 --> 00:07:12,609 That's exactly how that would work. So 141 00:07:12,839 --> 00:07:15,679 once again, you want to adopt this 142 00:07:15,790 --> 00:07:17,869 professional approach towards 143 00:07:18,040 --> 00:07:19,279 constantly 144 00:07:19,380 --> 00:07:21,839 making sure that your blocks of code. 145 00:07:22,140 --> 00:07:24,869 Whenever there's a possibility of an error occurring, 146 00:07:25,100 --> 00:07:27,500 they have been designed in such a way 147 00:07:27,570 --> 00:07:30,410 that they're able to handle any potential errors 148 00:07:30,559 --> 00:07:35,160 by making use of the try except block. And then for the accept statement itself, 149 00:07:35,369 --> 00:07:38,059 you can specify the exact kind of error 150 00:07:38,339 --> 00:07:44,079 that you expect might occur. Thank you for watching. I will see you in the next class.