1 00:00:00,210 --> 00:00:01,680 Instructor: Error handling allows us 2 00:00:01,680 --> 00:00:04,260 to let the script continue running 3 00:00:04,260 --> 00:00:06,570 even if there is an error. 4 00:00:06,570 --> 00:00:07,980 Let's try an example 5 00:00:07,980 --> 00:00:10,533 to show you how we can do error handling in Python. 6 00:00:11,910 --> 00:00:14,520 Let's say we wanted to create a program 7 00:00:14,520 --> 00:00:17,370 that allows us to take a users input. 8 00:00:17,370 --> 00:00:19,770 Let's say that we ask the user for an age. 9 00:00:19,770 --> 00:00:24,333 So, we use the input prompt and say, what is your age? 10 00:00:26,130 --> 00:00:26,963 And then here, 11 00:00:26,963 --> 00:00:31,200 what we're going to do is just print the age. 12 00:00:31,200 --> 00:00:33,120 Nice and simple program. 13 00:00:33,120 --> 00:00:34,360 If I run this 14 00:00:35,610 --> 00:00:38,250 and I type in let's say 10, 15 00:00:38,250 --> 00:00:41,490 there you go, everything is working. 16 00:00:41,490 --> 00:00:46,353 But what if I didn't enter a number and I go like this? 17 00:00:47,580 --> 00:00:48,720 Well, that still works 18 00:00:48,720 --> 00:00:53,160 but is that what I wanted to do with my program? 19 00:00:53,160 --> 00:00:56,040 Maybe I wanted to enter the age 20 00:00:56,040 --> 00:00:58,920 and calculate if they can play the game or not, 21 00:00:58,920 --> 00:01:01,530 whether they are 18 and over. 22 00:01:01,530 --> 00:01:02,703 How can we fix this? 23 00:01:03,660 --> 00:01:05,250 Well, one thing we can do 24 00:01:05,250 --> 00:01:08,520 is just to wrap this in an int, right? 25 00:01:08,520 --> 00:01:12,330 So, we convert whatever the user gives us into an integer. 26 00:01:12,330 --> 00:01:13,630 So that if I run this 27 00:01:15,030 --> 00:01:16,173 and I type this, 28 00:01:17,117 --> 00:01:20,490 uh-oh, I get an error, a value error, 29 00:01:20,490 --> 00:01:22,950 invalid literal for int. 30 00:01:22,950 --> 00:01:27,950 I'm trying to convert this into an integer 31 00:01:28,050 --> 00:01:30,960 but it's telling me that's an error, I can't do that. 32 00:01:30,960 --> 00:01:35,310 So how can we fix this with error handling? 33 00:01:35,310 --> 00:01:38,163 In Python, we can do it this way. 34 00:01:39,240 --> 00:01:41,040 We have the try. 35 00:01:41,040 --> 00:01:41,873 Oops. 36 00:01:41,873 --> 00:01:44,550 We have the try keyword which says, 37 00:01:44,550 --> 00:01:48,450 hey, I want you try this piece of code 38 00:01:48,450 --> 00:01:50,350 and we have to make sure we indent it. 39 00:01:51,600 --> 00:01:53,883 So, we try this piece of code. 40 00:01:54,990 --> 00:01:58,143 Now, anything that happens inside of the try block, 41 00:01:59,280 --> 00:02:00,993 I'm able to handle. 42 00:02:02,340 --> 00:02:04,980 See, where handling is coming from errors 43 00:02:04,980 --> 00:02:07,923 with the except keyword. 44 00:02:09,000 --> 00:02:11,880 And except says, 45 00:02:11,880 --> 00:02:16,880 hey, if whatever's in here in the try block, 46 00:02:17,550 --> 00:02:22,050 if anything errors out, I want you to say something. 47 00:02:22,050 --> 00:02:24,840 So instead of having this red error 48 00:02:24,840 --> 00:02:26,370 that exits out of the program, 49 00:02:26,370 --> 00:02:28,263 do something else, do this. 50 00:02:29,130 --> 00:02:30,393 So I'm going to say, 51 00:02:31,650 --> 00:02:35,433 print please enter a number. 52 00:02:37,500 --> 00:02:39,390 Let's see what happens now. 53 00:02:39,390 --> 00:02:43,800 If I click run, what is my age? 54 00:02:43,800 --> 00:02:45,500 I'm going to just write gibberish. 55 00:02:46,470 --> 00:02:47,730 And look at that. 56 00:02:47,730 --> 00:02:49,593 I get, please enter a number. 57 00:02:50,460 --> 00:02:52,770 I am handling my errors here 58 00:02:52,770 --> 00:02:57,770 by wrapping all of my code in a try block. 59 00:02:57,840 --> 00:03:00,780 So I could have just copy it my entire file here 60 00:03:00,780 --> 00:03:02,970 put it inside of a try block. 61 00:03:02,970 --> 00:03:04,560 And what's going to happen 62 00:03:04,560 --> 00:03:07,650 is instead of my program erroring out 63 00:03:07,650 --> 00:03:10,110 before my program crashes, 64 00:03:10,110 --> 00:03:12,720 it's going to run this code and then it's going to say, 65 00:03:12,720 --> 00:03:16,890 hey, if within this block anything happens, 66 00:03:16,890 --> 00:03:20,550 well, catch it in here with the except block 67 00:03:20,550 --> 00:03:25,470 and I can hear and do whatever I want. 68 00:03:25,470 --> 00:03:28,680 In my case, I can say, hey, please enter a number. 69 00:03:28,680 --> 00:03:30,660 But we have our problem here, right? 70 00:03:30,660 --> 00:03:33,600 The program just ended and we want this to keep running. 71 00:03:33,600 --> 00:03:38,253 I have to click run again and ask for the age once again. 72 00:03:39,510 --> 00:03:42,520 So, how can we make sure that we keep running this 73 00:03:43,380 --> 00:03:46,563 until the user actually enters something valid? 74 00:03:47,970 --> 00:03:49,710 Well, we've seen this before. 75 00:03:49,710 --> 00:03:52,653 We can do a while loop and say, while true. 76 00:03:54,120 --> 00:03:56,043 Run this. 77 00:03:58,830 --> 00:04:01,083 And if this works, 78 00:04:02,580 --> 00:04:06,300 well, we're gonna, hopefully, get the age. 79 00:04:06,300 --> 00:04:07,133 Let's see that. 80 00:04:07,133 --> 00:04:09,660 If I click run, what's my age? 81 00:04:09,660 --> 00:04:10,550 Let's say... 82 00:04:12,570 --> 00:04:14,400 Nope, please enter a number. 83 00:04:14,400 --> 00:04:15,270 What's your age? 84 00:04:15,270 --> 00:04:16,649 Let's try to break it again. 85 00:04:16,649 --> 00:04:18,329 Nope, please enter a number. 86 00:04:18,329 --> 00:04:19,200 All right, fine. 87 00:04:19,200 --> 00:04:20,463 My age is 10. 88 00:04:21,510 --> 00:04:23,850 There you go. I get the name 10, but... 89 00:04:23,850 --> 00:04:27,093 Uh-oh, it's still asking me what's my age. 90 00:04:29,250 --> 00:04:33,900 Ideally, I wanna break out of this true statement, right? 91 00:04:33,900 --> 00:04:36,030 So, how can I do that? 92 00:04:36,030 --> 00:04:41,030 Well, I can use what we call in here an else block, 93 00:04:41,100 --> 00:04:42,300 which we've seen before 94 00:04:42,300 --> 00:04:44,970 but this works with a try except block. 95 00:04:44,970 --> 00:04:47,160 We put it at the end. 96 00:04:47,160 --> 00:04:49,440 It says, hey, try this. 97 00:04:49,440 --> 00:04:51,270 If there's an error, do this. 98 00:04:51,270 --> 00:04:56,270 However, if there is no error, else, do this. 99 00:04:56,760 --> 00:04:59,703 So in our case, we can just say, break. 100 00:05:00,600 --> 00:05:01,433 And you know what? 101 00:05:01,433 --> 00:05:05,640 We can even print, thank you, just so we're nice . 102 00:05:05,640 --> 00:05:10,593 If I stop this and run again and I give them my age, 103 00:05:12,060 --> 00:05:13,290 there you go. 104 00:05:13,290 --> 00:05:18,290 We're done. We broke out of the loop and everything is good. 105 00:05:19,830 --> 00:05:24,060 And this try, except, else block can go anywhere. 106 00:05:24,060 --> 00:05:29,060 We can wrap our entire file in a try, except, else block. 107 00:05:29,070 --> 00:05:31,710 We can just add it into our functions, 108 00:05:31,710 --> 00:05:34,470 inside of our each individual functions. 109 00:05:34,470 --> 00:05:36,480 Whatever we want, we can use. 110 00:05:36,480 --> 00:05:39,480 This is just another tool that Python gives us, 111 00:05:39,480 --> 00:05:41,193 to handle errors. 112 00:05:42,420 --> 00:05:44,610 But let me ask you something. 113 00:05:44,610 --> 00:05:46,740 What if I did, instead of print age, 114 00:05:46,740 --> 00:05:51,740 I did let's say, 10 divided by the age. 115 00:05:54,090 --> 00:05:55,720 If I run my program 116 00:05:57,150 --> 00:05:59,640 and I type in here zero, 117 00:05:59,640 --> 00:06:00,513 and I enter, 118 00:06:02,183 --> 00:06:05,700 uh-oh, I get an error, please enter a number. 119 00:06:05,700 --> 00:06:07,653 But I did enter a number. 120 00:06:09,510 --> 00:06:11,910 This is a buggy program. 121 00:06:11,910 --> 00:06:15,090 Although I'm typing a number, 122 00:06:15,090 --> 00:06:17,850 it's still giving me the same error message. 123 00:06:17,850 --> 00:06:19,170 So, how do we handle this? 124 00:06:19,170 --> 00:06:21,030 How do we handle different errors? 125 00:06:21,030 --> 00:06:25,260 One where it is a value error 126 00:06:25,260 --> 00:06:29,523 and another one that is obviously, a zero division error. 127 00:06:30,900 --> 00:06:33,990 Well, in the except block 128 00:06:33,990 --> 00:06:38,640 I can simply give it what type of error I wanna handle. 129 00:06:38,640 --> 00:06:41,130 And remember, these are built-in errors in here 130 00:06:41,130 --> 00:06:42,780 that I can just copy and paste. 131 00:06:42,780 --> 00:06:47,280 So in my case, I wanna say this is for a value error. 132 00:06:47,280 --> 00:06:49,360 So that if I run my program 133 00:06:50,280 --> 00:06:51,633 and I type in zero, 134 00:06:52,909 --> 00:06:55,320 uh-oh, I get my error. 135 00:06:55,320 --> 00:06:59,070 This except block only accepts value errors. 136 00:06:59,070 --> 00:07:02,640 Any other errors, it does not care about. 137 00:07:02,640 --> 00:07:04,893 Hmm. So what can we do here? 138 00:07:05,940 --> 00:07:10,110 Well, one option is to copy here and say, 139 00:07:10,110 --> 00:07:12,300 hey, let's add another except. 140 00:07:12,300 --> 00:07:15,990 And in this case, I want a zero division error. 141 00:07:15,990 --> 00:07:19,920 In case a user enters zero, you can say, 142 00:07:19,920 --> 00:07:24,920 Please enter age higher than zero. 143 00:07:27,750 --> 00:07:32,750 So that if I run this now and I do zero, 144 00:07:32,820 --> 00:07:35,610 I get, please enter age higher than zero. 145 00:07:35,610 --> 00:07:38,220 If I do some gibberish... 146 00:07:38,220 --> 00:07:39,053 Oops. 147 00:07:39,053 --> 00:07:42,690 If I do some gibberish, I get, please enter a number. 148 00:07:42,690 --> 00:07:44,970 If I finally enter a number, look at that. 149 00:07:44,970 --> 00:07:47,343 Thank you. Everything is working. 150 00:07:48,960 --> 00:07:50,160 Now, here's my question. 151 00:07:50,160 --> 00:07:55,160 What happens if we enter another value error here? 152 00:07:56,070 --> 00:08:00,660 And this value error is just going to say exclamation marks. 153 00:08:00,660 --> 00:08:01,653 What happens now? 154 00:08:06,120 --> 00:08:08,820 I get please enter a number. 155 00:08:08,820 --> 00:08:12,990 because this except block is going to run only once. 156 00:08:12,990 --> 00:08:15,600 As soon as it catches the error it's looking for, 157 00:08:15,600 --> 00:08:20,283 it's going to run and then come back to the while loop. 158 00:08:22,170 --> 00:08:25,830 So only one of the errors will be caught. 159 00:08:25,830 --> 00:08:27,180 Okay, let's take a break 160 00:08:27,180 --> 00:08:30,327 and learn more about error handling in the next video. 161 00:08:30,327 --> 00:08:31,233 Bye-bye.