1 00:00:00,210 --> 00:00:05,830 Air handling allows us to let the script continue running, even if there is an error. 2 00:00:06,570 --> 00:00:10,440 Let's try an example to show you how we can do air handling and python. 3 00:00:11,870 --> 00:00:17,250 Let's say we wanted to create a program that allows us to take a user's input. 4 00:00:17,390 --> 00:00:19,690 Let's say that we ask the user for an H. 5 00:00:19,790 --> 00:00:24,170 So we use the input prompt and say, what is your age? 6 00:00:26,100 --> 00:00:34,260 And then here or we're going to do is just print the nice and simple program if I run this. 7 00:00:35,530 --> 00:00:37,390 And I type in, let's say, 10. 8 00:00:38,230 --> 00:00:38,860 There you go. 9 00:00:39,130 --> 00:00:46,180 Everything is working, but what if I didn't enter a number and I go like this? 10 00:00:47,610 --> 00:00:54,960 Well, that still works, but is that what I wanted to do with my program, maybe I wanted to enter 11 00:00:54,960 --> 00:01:01,890 the age and calculate if they can play the game of not or not, whether they're 18 and over, how can 12 00:01:01,890 --> 00:01:02,550 we fix this? 13 00:01:03,640 --> 00:01:10,120 Well, one thing we can do is just to wrap this in an ENT, right, so we convert whatever the user 14 00:01:10,120 --> 00:01:13,540 gives us into an integer so that if I run this. 15 00:01:14,980 --> 00:01:16,000 And I type this. 16 00:01:17,080 --> 00:01:24,820 Oh, I get an error, a value error invalid litoral for int, I'm trying to convert. 17 00:01:25,820 --> 00:01:30,390 This into an integer, but it's telling me that's an error, I can't do that. 18 00:01:30,980 --> 00:01:36,100 So how can we fix this with error handling in Python? 19 00:01:36,620 --> 00:01:38,050 We can do it this way. 20 00:01:39,250 --> 00:01:47,860 We have the tri loops, we have the tri keyboard, which says, hey, I want you to try this piece of 21 00:01:47,860 --> 00:01:50,020 code and we have to make sure we invented. 22 00:01:51,610 --> 00:01:53,770 So we try this piece of code. 23 00:01:54,970 --> 00:01:58,030 Now, anything that happens inside of the tri block. 24 00:01:59,250 --> 00:02:00,840 I'm able to handle. 25 00:02:02,320 --> 00:02:07,780 See where handling is coming from, areas with the accept keyword. 26 00:02:13,870 --> 00:02:21,310 If whatever's in here in the tri block, if anything airs out, I want you to say something. 27 00:02:22,060 --> 00:02:27,460 So instead of having this red air that exits out of the program, do something else. 28 00:02:27,460 --> 00:02:28,090 Do this. 29 00:02:29,110 --> 00:02:30,220 So I'm going to say. 30 00:02:31,670 --> 00:02:35,270 Print, please enter a number. 31 00:02:37,480 --> 00:02:38,490 Let's see what happens now. 32 00:02:39,380 --> 00:02:40,670 If I click, run. 33 00:02:42,620 --> 00:02:43,460 What is my age? 34 00:02:43,760 --> 00:02:45,340 I'm going to just write gibberish. 35 00:02:46,430 --> 00:02:49,490 And look at that, I get please enter a number. 36 00:02:50,410 --> 00:02:59,350 I am handling my errors here by wrapping all of my code in a try block so I could have just copy Basehead 37 00:02:59,350 --> 00:03:02,320 my entire file here, put it inside of a try block. 38 00:03:02,920 --> 00:03:10,570 And what's going to happen is instead of my program airing out before my program crashes, it's going 39 00:03:10,570 --> 00:03:17,800 to run this code and then it's going to say, hey, if within this block anything happens, we'll catch 40 00:03:17,800 --> 00:03:20,020 it in here with the except block. 41 00:03:20,500 --> 00:03:22,120 And I can hear. 42 00:03:23,570 --> 00:03:29,600 Do whatever I want in my case, I can say, hey, please enter our number, but we have our problem 43 00:03:29,600 --> 00:03:30,010 here, right? 44 00:03:30,670 --> 00:03:33,610 The program just ended and we want this to keep running. 45 00:03:33,620 --> 00:03:38,090 I have to run again and ask for the H once again. 46 00:03:39,530 --> 00:03:46,460 So how can you make sure that we keep running this until while that user actually enter something fairly? 47 00:03:47,970 --> 00:03:52,500 Well, we've seen this before, we can do a while loop and say while true. 48 00:03:54,100 --> 00:03:55,870 Ron, this. 49 00:03:58,860 --> 00:04:00,900 And if this works. 50 00:04:02,570 --> 00:04:03,560 Well, we're going to. 51 00:04:04,470 --> 00:04:10,200 Hopefully get the H, let's see that if I click run was my age, let's say. 52 00:04:12,530 --> 00:04:15,050 No, please enter a no what's your age? 53 00:04:15,290 --> 00:04:16,570 Let's try to break it again. 54 00:04:16,640 --> 00:04:18,050 No, please enter our number. 55 00:04:18,290 --> 00:04:18,950 All right, fine. 56 00:04:19,190 --> 00:04:20,330 My age is 10. 57 00:04:21,500 --> 00:04:26,930 There you go, I get the name 10, but, oh, it's still ask me what's my age. 58 00:04:29,200 --> 00:04:35,140 Ideally, I want to break out of this true statement, right, so how can I do that? 59 00:04:36,010 --> 00:04:42,160 Well, I can use what we call in here and else block, which we've seen before. 60 00:04:42,310 --> 00:04:44,740 But this works with a try except block. 61 00:04:44,980 --> 00:04:46,870 We put it at the end. 62 00:04:47,140 --> 00:04:49,030 It says, hey, try this. 63 00:04:49,420 --> 00:04:51,100 If there's an ne'er do this. 64 00:04:51,250 --> 00:04:56,130 However, if there is nowhere else, do this. 65 00:04:56,740 --> 00:04:59,560 So in our case, we can just say break. 66 00:05:00,560 --> 00:05:08,380 And you know what, we can even print thank you, just so we're nice if I stop this and run again. 67 00:05:09,350 --> 00:05:10,450 And I give them my age. 68 00:05:12,060 --> 00:05:12,580 There you go. 69 00:05:13,290 --> 00:05:18,540 We're done, we broke out of the loop and everything is good. 70 00:05:19,810 --> 00:05:28,630 And this tri except else block can go anywhere, we can wrap our entire file in a try except block, 71 00:05:29,050 --> 00:05:34,150 we can just add it into our functions inside of our each individual functions. 72 00:05:34,450 --> 00:05:36,490 Whatever we want, we can use. 73 00:05:36,520 --> 00:05:41,020 This is just another tool that Python gives us to handle Eres. 74 00:05:42,390 --> 00:05:44,050 But let me ask you something. 75 00:05:44,610 --> 00:05:51,810 What if I did instead of printing, which I did, let's say ten divided by the age? 76 00:05:54,080 --> 00:05:55,670 If I run my program. 77 00:05:57,120 --> 00:06:00,360 And I type in here zero and I enter. 78 00:06:02,150 --> 00:06:07,490 Oh, I get in there, please enter a number, but I did enter a number. 79 00:06:09,520 --> 00:06:17,450 This is a buggy program, although I'm typing a number, it's still giving me the same error message. 80 00:06:17,860 --> 00:06:19,170 So how do we handle this? 81 00:06:19,180 --> 00:06:27,640 How do we handle different errors, one where it is a value error and another one that is obviously 82 00:06:27,640 --> 00:06:29,380 a zero division error? 83 00:06:30,860 --> 00:06:39,440 Well, in the except block, I can simply give it what type of error I want to handle and remember, 84 00:06:39,440 --> 00:06:42,410 these are built in errors in here that I can just copy and paste. 85 00:06:42,800 --> 00:06:49,310 So in my case, I want to say this is for a value error so that if I run my program. 86 00:06:50,240 --> 00:06:51,500 And I type in zero. 87 00:06:52,940 --> 00:07:01,850 Oh, I get my air, this except block only accepts valuers, any other errors it does not care about. 88 00:07:02,660 --> 00:07:03,020 Hmm. 89 00:07:03,320 --> 00:07:04,760 So what can we do here? 90 00:07:05,930 --> 00:07:13,730 Well, one option is to copy here and say, hey, let's add another, except and in this case, I want 91 00:07:13,730 --> 00:07:25,310 a zero division error in case a user enters zero, you can say please enter age higher than zero. 92 00:07:27,770 --> 00:07:29,720 So that if I run this now. 93 00:07:30,870 --> 00:07:40,230 And I do zero again, please enter higher than zero if I do some gibberish, oops, if I do some gibberish 94 00:07:41,070 --> 00:07:44,350 I get please enter a number if I finally enter a number. 95 00:07:44,400 --> 00:07:44,990 Look at that. 96 00:07:45,000 --> 00:07:45,510 Thank you. 97 00:07:45,570 --> 00:07:47,250 Everything is working. 98 00:07:48,920 --> 00:07:52,220 Now, here's my question, what happens if we enter? 99 00:07:53,570 --> 00:07:59,910 Another value error here and this value error is just going to say exclamation marks. 100 00:08:00,650 --> 00:08:01,480 What happens now? 101 00:08:06,110 --> 00:08:14,300 I get please enter a number, because this except block is going to run only once, as soon as it catches 102 00:08:14,300 --> 00:08:16,700 the air, it's looking for it's going to run. 103 00:08:18,030 --> 00:08:20,160 And then come back to the wire loop. 104 00:08:22,140 --> 00:08:24,900 So only one of the heirs will be caught. 105 00:08:25,800 --> 00:08:30,110 OK, let's take a break and learn more about air handling in the next video. 106 00:08:30,880 --> 00:08:31,030 Bye.