1 00:00:04,870 --> 00:00:06,590 Handling exceptions. 2 00:00:07,120 --> 00:00:07,850 Hello, everyone. 3 00:00:07,870 --> 00:00:13,150 Today, we're going to learn very interesting things, the first thing and actually the method of this 4 00:00:13,150 --> 00:00:21,010 video is how to handle exceptions and also teach you how to create a custom python file and then run 5 00:00:21,010 --> 00:00:28,300 it from the terminal so you can write a huge amount of code to which later you can simply wrong. 6 00:00:28,750 --> 00:00:35,860 So as you can see, I already created my computer folder called Section two, which is actually the 7 00:00:35,860 --> 00:00:39,370 section in how we structure the sections in discourse. 8 00:00:39,610 --> 00:00:44,900 So in that folder, which specifically for that section, I will actually create another folder called 9 00:00:44,930 --> 00:00:45,790 Resources. 10 00:00:48,950 --> 00:00:56,600 OK, so let's introduce further and let's create a text file, so Olmec, I usually use text editor, 11 00:00:56,600 --> 00:00:59,100 but feel free to use a dictator. 12 00:00:59,120 --> 00:01:03,460 I think Windows is not bad to create a text file. 13 00:01:04,220 --> 00:01:11,330 So here I will create a new document and I will save it as. 14 00:01:13,630 --> 00:01:17,140 Exceptions that few white. 15 00:01:19,510 --> 00:01:24,580 And then the only thing I need to do here is just write. 16 00:01:26,800 --> 00:01:33,910 Don't peel away after it, this is select use, pure white, so if you're a Mac, you need to do the 17 00:01:33,910 --> 00:01:34,510 same thing. 18 00:01:34,510 --> 00:01:38,520 But if you use other devices, you're not going to do that. 19 00:01:38,860 --> 00:01:40,090 So when you open the file. 20 00:01:41,600 --> 00:01:48,440 In your preferred text editor actually here, we're going to use by chance, so I will run it all right 21 00:01:48,560 --> 00:01:50,620 by charm. 22 00:01:51,860 --> 00:01:59,900 So here, once you enter Patro, you can simply select open and go to the file that actually won't open. 23 00:02:00,560 --> 00:02:04,650 So here is the file exceptionals be and select open. 24 00:02:04,670 --> 00:02:09,590 So now you can edit it with a or actually any editor that you would like to use. 25 00:02:10,640 --> 00:02:13,280 So let's right here import. 26 00:02:15,400 --> 00:02:20,830 Eurorail library, not arrow. 27 00:02:22,730 --> 00:02:33,650 And then I will write from you you library to request, and from here I will report that you are ale 28 00:02:34,310 --> 00:02:39,880 open so we can actually open a euro and here's how we do the exceptions. 29 00:02:40,610 --> 00:02:46,760 So first of all, let me tell you why we actually need to do exceptions basically in every programming 30 00:02:46,760 --> 00:02:47,270 language. 31 00:02:47,270 --> 00:02:49,470 And Python is no different. 32 00:02:49,940 --> 00:02:56,150 We need to do exceptions in order to be sure that the program runs correctly, even if we have some 33 00:02:56,150 --> 00:02:56,720 errors. 34 00:02:57,560 --> 00:03:06,660 So it is always better to create an exception for an expected error in order to show the user the exact 35 00:03:06,660 --> 00:03:10,100 quantification and the exact information for fixing that error. 36 00:03:10,730 --> 00:03:13,320 So here's what we do first. 37 00:03:13,370 --> 00:03:13,780 All right. 38 00:03:13,790 --> 00:03:14,330 Tried. 39 00:03:15,960 --> 00:03:16,530 OK. 40 00:03:18,400 --> 00:03:23,670 And try means that are going to try some common, so we will try you, R.L.. 41 00:03:24,830 --> 00:03:25,520 Open. 42 00:03:27,730 --> 00:03:33,820 And then the name of the website and here I prepared one website that has on the American side. 43 00:03:33,890 --> 00:03:42,010 So I go to HTP and the website is called w w w dot i e. 44 00:03:43,170 --> 00:03:43,950 T.F.. 45 00:03:45,020 --> 00:03:46,240 Dot org. 46 00:03:48,200 --> 00:03:50,510 Ah, F.C.. 47 00:03:51,770 --> 00:03:55,180 RNC zero to 60. 48 00:03:58,290 --> 00:04:04,770 OK, so this is the Web page that we're going to assess and in case anything gets wrong and we are not 49 00:04:04,770 --> 00:04:12,000 able to open it, let's write except the URL or. 50 00:04:14,800 --> 00:04:15,580 Library. 51 00:04:17,010 --> 00:04:25,090 Dot ever dot h t v error as e. 52 00:04:25,510 --> 00:04:32,820 OK, so if we get this area here a lot, you can see from there a library in the cold below this area 53 00:04:32,820 --> 00:04:37,230 is going to be expressed with a letter E so I can write print. 54 00:04:40,370 --> 00:04:44,570 And in parentheses, exception. 55 00:04:46,300 --> 00:04:50,470 Slushy, sort of Komaki then print. 56 00:04:55,250 --> 00:04:56,210 Status. 57 00:04:58,640 --> 00:05:01,010 Splash eat dogs called. 58 00:05:02,200 --> 00:05:07,900 OK, then we'll print the reason for their sole reason. 59 00:05:10,880 --> 00:05:21,740 There's reason, Cuomo idot reason and finally will print. 60 00:05:22,950 --> 00:05:28,560 The last line, which will be You are OK and I'll write E! 61 00:05:30,340 --> 00:05:31,560 That you are. 62 00:05:32,540 --> 00:05:40,330 OK, so here, if we get this type of error HTP error, then we're going to show plenty of information 63 00:05:40,330 --> 00:05:40,840 for it. 64 00:05:41,110 --> 00:05:45,180 Instead of simply displaying the error for the user, let's save that. 65 00:05:45,340 --> 00:05:49,930 So I would control errors and let's actually run the code. 66 00:05:50,470 --> 00:05:55,240 So I'll simply go to the terminal and I'll go to the folder. 67 00:05:55,270 --> 00:05:59,830 So so I will go to Section two. 68 00:06:01,920 --> 00:06:08,590 And with and here we have the resources, the resources folder, and you can see the exception, why 69 00:06:08,590 --> 00:06:13,630 is there so you can simply do Python and do the right exceptional way. 70 00:06:14,080 --> 00:06:20,090 So while around the fire, you can see that we've got the exception, HTP error. 71 00:06:21,200 --> 00:06:21,760 OK. 72 00:06:22,850 --> 00:06:23,660 And. 73 00:06:25,890 --> 00:06:28,090 This means that the page is not found. 74 00:06:28,160 --> 00:06:34,230 OK, so this is what he would get if we don't add the additional lines with the exception, but rather 75 00:06:34,230 --> 00:06:39,090 the exception, we can see the status is 404, which is called. 76 00:06:39,390 --> 00:06:46,910 The reason is that it is not found the Eurorail, because here you can see we not have the euro. 77 00:06:47,220 --> 00:06:48,600 The euro is the. 78 00:06:50,120 --> 00:06:59,450 This one, the IETF dot org, so you can see all this information and you can actually add more information 79 00:06:59,450 --> 00:07:05,930 if you desire, so you can see this for less, of course, when you get on there and see if we do not 80 00:07:05,930 --> 00:07:06,800 have the exception. 81 00:07:07,010 --> 00:07:13,460 For example, if we don't choose this quote, but they simply write, you are open. 82 00:07:14,390 --> 00:07:17,140 And then right now the U.S. actually copy. 83 00:07:18,860 --> 00:07:25,700 So if I just copy this here, I basically get in some mode and they see nothing about the fact that 84 00:07:25,700 --> 00:07:31,750 we actually got any error related to what we just did. 85 00:07:32,270 --> 00:07:35,870 But since we get the exception, we have this exception. 86 00:07:36,140 --> 00:07:40,000 If any issue happens, you can always see the error message. 87 00:07:40,010 --> 00:07:43,620 And what are the details regarding that message? 88 00:07:43,640 --> 00:07:44,750 So this is quite useful. 89 00:07:45,290 --> 00:07:48,550 So this is everything I want to share with you guys about exceptions. 90 00:07:48,800 --> 00:07:55,460 I hope you enjoyed this video and you actually learn how to run Python codes, which in your term know 91 00:07:55,460 --> 00:07:59,210 machine, which quite well, because we are going to use this extra quite a lot. 92 00:07:59,710 --> 00:08:01,660 That said, thank you very much for watching, guys. 93 00:08:01,880 --> 00:08:06,570 And tomorrow we'll continue with that with the HTP Heathers. 94 00:08:06,980 --> 00:08:07,940 Thanks for watching.