1 00:00:00,000 --> 00:00:01,600 [no audio] 2 00:00:01,600 --> 00:00:03,200 Friends here we are going to 3 00:00:03,200 --> 00:00:05,900 discuss about exception handling with your 4 00:00:05,900 --> 00:00:11,700 Python. First of all, what is an exception? See simply, simply 5 00:00:11,700 --> 00:00:18,100 exception is nothing but an error and a Python program or 6 00:00:18,100 --> 00:00:24,300 script terminates as soon as it encounters an error. Assume 7 00:00:24,300 --> 00:00:27,200 that you have some ten or hundred or thousands of lines in 8 00:00:27,200 --> 00:00:28,200 your Python Script. 9 00:00:28,200 --> 00:00:30,200 [no audio] 10 00:00:30,200 --> 00:00:32,500 Maybe you have somewhere in your code error. 11 00:00:33,100 --> 00:00:35,600 If that error is encountered by your Python, 12 00:00:35,600 --> 00:00:39,400 then your script will stop. Your script won't execute. 13 00:00:39,400 --> 00:00:41,200 It will terminate, right. 14 00:00:41,500 --> 00:00:43,300 See let me show you with simple example. 15 00:00:43,300 --> 00:00:48,700 Let me save it as 'working_with_exceptions.py'. 16 00:00:50,400 --> 00:00:53,400 Now guys, first of all I am writing simply, see that 17 00:00:53,400 --> 00:00:58,500 [no audio] 18 00:00:58,500 --> 00:00:59,500 "Welcome to 19 00:00:59,500 --> 00:01:02,700 [no audio] 20 00:01:02,700 --> 00:01:05,400 exceptions concept". 21 00:01:05,400 --> 00:01:08,400 [no audio] 22 00:01:08,400 --> 00:01:09,700 Fine, let me run it. 23 00:01:11,300 --> 00:01:14,900 Yes, you are getting your output. Right. Assume that you 24 00:01:14,900 --> 00:01:16,700 have only one line in your code. No problem. 25 00:01:16,800 --> 00:01:20,300 Let me remove this parentheses at the end of your 'print'. Now 26 00:01:20,300 --> 00:01:25,100 see the result. You're getting an error. Because of that error 27 00:01:25,200 --> 00:01:27,500 you are going to stop your Python Script. 28 00:01:28,600 --> 00:01:31,000 Assume that you have one more line then. I will do one more 29 00:01:31,000 --> 00:01:33,100 thing. First of all 30 00:01:34,300 --> 00:01:35,300 I'm having. 31 00:01:35,300 --> 00:01:42,100 [no audio] 32 00:01:42,100 --> 00:01:44,400 Now it is fine. 33 00:01:44,700 --> 00:01:45,900 Let me save it and run it. 34 00:01:46,400 --> 00:01:48,300 Yes, you're getting two lines of output. 35 00:01:48,600 --> 00:01:51,200 But in the very first line what I am doing is, I am doing a mistake. 36 00:01:51,200 --> 00:01:53,000 [no audio] 37 00:01:53,000 --> 00:01:54,600 Now, you're not getting output for 38 00:01:56,000 --> 00:01:57,000 second line 39 00:01:57,000 --> 00:01:59,100 [no audio] 40 00:01:59,100 --> 00:02:05,600 See, while completion of your first line you did some mistake, right, 41 00:02:05,600 --> 00:02:07,700 because of that output, what is output you're getting? 42 00:02:07,700 --> 00:02:12,300 "SyntaxError". So you have some error somewhere. Because of that 43 00:02:12,300 --> 00:02:18,900 error your Python is going to stop your script. Right. Fine. 44 00:02:19,100 --> 00:02:21,600 Now, let me do it. Now, it's fine. 45 00:02:21,600 --> 00:02:27,000 Now, what I am doing is, 'print(, some 4/, or 4/2. 46 00:02:27,300 --> 00:02:30,300 Yes, you're getting result as 2.0. That's fine. 47 00:02:30,900 --> 00:02:32,400 Now instead of 2 I'm taking 0. 48 00:02:33,300 --> 00:02:34,300 Now see the result. 49 00:02:34,300 --> 00:02:36,300 [no audio] 50 00:02:36,300 --> 00:02:40,200 Again, you're also getting an error. That error is 'ZeroDivisionError: '. 51 00:02:40,200 --> 00:02:41,700 'division by zero'. 52 00:02:43,400 --> 00:02:48,300 See guys, if you compare in this way, let me remove here. 53 00:02:49,800 --> 00:02:52,700 What is output you're getting? 'SyntaxError'. 54 00:02:53,300 --> 00:02:58,200 Now if I do this thing, I mean divisible with 0, 55 00:02:58,200 --> 00:03:00,100 now you are getting 'ZeroDivisionError'. 56 00:03:00,800 --> 00:03:04,100 See previously you are getting 'SyntaxError', but now you are getting 57 00:03:04,100 --> 00:03:05,100 'ZeroDivisionError'. 58 00:03:05,800 --> 00:03:09,100 So basically errors are two types. 59 00:03:10,200 --> 00:03:11,200 In your Python, 60 00:03:12,400 --> 00:03:14,800 errors are two types. 61 00:03:15,800 --> 00:03:17,000 What they are? 62 00:03:17,700 --> 00:03:18,700 First one is 63 00:03:19,700 --> 00:03:23,300 'SyntaxError' or 'SyntaxErrors' 64 00:03:23,300 --> 00:03:26,500 Second one is, see guys, 65 00:03:26,600 --> 00:03:30,600 if you observe 4/2, it's working perfectly. 66 00:03:30,900 --> 00:03:32,400 That means perfect syntax 67 00:03:32,400 --> 00:03:36,400 is there. If I do with 0 as per syntax, it's correct, right? 68 00:03:37,400 --> 00:03:39,900 Maybe as per your mathematic rule it is not possible, 69 00:03:40,900 --> 00:03:42,600 but as per syntax it is correct. 70 00:03:43,700 --> 00:03:48,400 Now, while running you are getting an error because of 4 71 00:03:48,400 --> 00:03:50,300 division with 0 is not possible. 72 00:03:50,300 --> 00:03:53,800 That means while running you're getting a different type 73 00:03:53,800 --> 00:03:55,600 of error called 'ZeroDivisionError'. 74 00:03:56,300 --> 00:03:59,400 Now this type of errors are called runtime errors. 75 00:04:00,300 --> 00:04:04,300 See as per syntax it is correct, but while running because you 76 00:04:04,300 --> 00:04:08,200 don't have any defined value for 4/0, right, 77 00:04:08,200 --> 00:04:09,300 that's why you are getting an error. 78 00:04:09,400 --> 00:04:11,200 That's why this is a runtime error. 79 00:04:11,200 --> 00:04:13,800 [no audio] 80 00:04:13,800 --> 00:04:17,300 And these errors are nothing but exceptions. 81 00:04:17,300 --> 00:04:21,000 [no audio] 82 00:04:21,000 --> 00:04:24,500 This is an exceptional case, right. As per mathematic rule 83 00:04:24,500 --> 00:04:26,100 if 4/2 is possible, 84 00:04:26,100 --> 00:04:28,900 sorry 4/2 is possible but only 4/0 85 00:04:28,900 --> 00:04:30,500 is not possible, some exception. 86 00:04:31,300 --> 00:04:36,700 So exceptions are occurring only during runtime, right. Fine. 87 00:04:37,200 --> 00:04:40,000 See this syntax errors, 88 00:04:40,900 --> 00:04:45,400 you don't have any special rule to handle this syntax errors. 89 00:04:45,900 --> 00:04:50,100 That means you need to follow the syntaxes exactly while 90 00:04:50,100 --> 00:04:52,800 writing your Python Script. No way 91 00:04:54,500 --> 00:05:02,600 to handle syntax errors, but you can handle exceptions. You 92 00:05:02,600 --> 00:05:07,100 can handle exceptions. That means, see as of now you're getting 93 00:05:07,100 --> 00:05:10,700 suppose 4/0, some 'ZeroDivisionError'. That type 94 00:05:10,700 --> 00:05:14,700 of errors you can handle and instead of terminating your 95 00:05:14,700 --> 00:05:16,000 Python Script in this way 96 00:05:16,100 --> 00:05:18,500 you can terminate with meaningful information. 97 00:05:18,500 --> 00:05:20,800 [no audio] 98 00:05:20,800 --> 00:05:25,200 Right. So I will show you that. Before that there is a way 99 00:05:25,200 --> 00:05:26,200 to handle, 100 00:05:26,200 --> 00:05:29,300 [no audio] 101 00:05:29,300 --> 00:05:36,300 it's important guys, to handle runtime errors or exceptions. 102 00:05:36,800 --> 00:05:38,500 Let me show you with simple example. 103 00:05:39,000 --> 00:05:40,800 See what I am doing is, 104 00:05:40,800 --> 00:05:44,000 [no audio] 105 00:05:44,000 --> 00:05:45,700 don't worry about syntax. 106 00:05:45,700 --> 00:05:46,700 Just observe the output. 107 00:05:46,700 --> 00:05:49,500 I am printing simply 4/2. 108 00:05:49,500 --> 00:05:54,000 [no audio] 109 00:05:54,000 --> 00:05:55,500 Now I am simply printing. 110 00:05:55,500 --> 00:06:05,400 [no audio] 111 00:06:05,400 --> 00:06:09,000 Now previously you are getting your output in this way, some random 112 00:06:09,000 --> 00:06:11,100 message you are getting. Now you are not going to get. You 113 00:06:11,100 --> 00:06:13,000 are going to get. See that result first. 114 00:06:13,700 --> 00:06:18,000 It's perfect. Suppose if I do 4/0, what is happening? 115 00:06:18,700 --> 00:06:20,300 It is not giving an error. 116 00:06:21,400 --> 00:06:24,000 Of course, this is the error but we are handling that. We are giving 117 00:06:24,000 --> 00:06:27,400 with clear information 'ZeroDivisionErrors', or 118 00:06:27,400 --> 00:06:31,800 'ZeroDivisionError'. Now it is not like an error. Of course 119 00:06:31,800 --> 00:06:37,800 it is an error, but we are handling it carefully. Right. Fine. 120 00:06:38,200 --> 00:06:39,200 So first of all 121 00:06:39,200 --> 00:06:41,800 [no audio] 122 00:06:41,800 --> 00:06:46,700 How, when, when you have to use exception handling 123 00:06:46,700 --> 00:06:48,200 in your Python Script? 124 00:06:49,300 --> 00:06:52,100 Nothing is there guys. Very simple. Assume that you have a 125 00:06:52,100 --> 00:06:58,100 code. Assume that you have a code, and if there is a chance 126 00:06:58,100 --> 00:07:01,400 to get any runtime error, then you have to handle it. 127 00:07:03,000 --> 00:07:04,300 Very simple thing. One more thing. 128 00:07:04,300 --> 00:07:05,300 Let me do one thing. 129 00:07:06,100 --> 00:07:07,300 I want to open a file. 130 00:07:07,800 --> 00:07:09,200 I want to open a file called 131 00:07:09,200 --> 00:07:15,500 let's say 'open nari.txt', for read purpose. 132 00:07:15,800 --> 00:07:16,600 That's my default mode 133 00:07:16,600 --> 00:07:19,700 I am not mentioning. Then I am closing. Between these two 134 00:07:19,700 --> 00:07:22,300 let me print 'fo.read'. 135 00:07:23,500 --> 00:07:24,800 See the output. What you are getting. 136 00:07:26,300 --> 00:07:27,900 'FileNotFoundError'. 137 00:07:28,800 --> 00:07:31,100 Actually 'nari.txt' file is existing, 138 00:07:31,100 --> 00:07:34,500 then you don't have any error. As per syntax it is correct, 139 00:07:34,500 --> 00:07:38,100 but while running because there is no file you are getting an error. 140 00:07:38,200 --> 00:07:39,600 That's why this is a runtime error. 141 00:07:40,100 --> 00:07:41,800 So here there is a chance. 142 00:07:43,000 --> 00:07:44,000 What is that chance? 143 00:07:44,000 --> 00:07:48,000 Maybe 'nari.txt' file exists or may not exist. 144 00:07:49,700 --> 00:07:52,500 Right. So that's why you can expect here, 145 00:07:52,700 --> 00:07:55,300 yes, there is a chance to get an exception. 146 00:07:55,500 --> 00:07:57,100 That's why you can handle in this way. 147 00:07:57,400 --> 00:07:58,400 What is that? 148 00:07:58,400 --> 00:08:01,600 'try:'. 'try:', open your file, 149 00:08:01,600 --> 00:08:05,400 [no audio] 150 00:08:05,400 --> 00:08:08,100 then let me print content, 151 00:08:08,100 --> 00:08:12,200 [no audio] 152 00:08:12,200 --> 00:08:16,600 then let me close our file. If really file is there this code 153 00:08:16,600 --> 00:08:17,900 will execute otherwise 154 00:08:19,400 --> 00:08:22,900 'except:' let me write some, I don't know 155 00:08:22,900 --> 00:08:23,500 what is the error. 156 00:08:23,500 --> 00:08:29,300 Let me write, "Some problem while reading the nari.txt 157 00:08:29,500 --> 00:08:30,500 file". That's it. 158 00:08:31,600 --> 00:08:32,600 Now, let me run it. 159 00:08:32,600 --> 00:08:34,900 [no audio] 160 00:08:34,900 --> 00:08:36,200 Yeah, now it is 'SyntaxError'. 161 00:08:36,600 --> 00:08:37,700 What is that syntax error? 162 00:08:37,700 --> 00:08:40,200 [no audio] 163 00:08:40,200 --> 00:08:44,500 Print. Yeah, it's not 'for', right. It is simply 'fo'. Yeah. 164 00:08:44,799 --> 00:08:46,299 You can't handle syntax errors. 165 00:08:46,700 --> 00:08:49,000 So be careful while writing your syntaxes. 166 00:08:49,900 --> 00:08:51,799 You can handle only your runtime errors. 167 00:08:52,000 --> 00:08:56,799 That's what here I'm showing. See actually 'nari.txt' 168 00:08:56,799 --> 00:08:57,700 file is not there. 169 00:08:57,800 --> 00:09:02,400 If it is there what will happen? This block will execute. Then 170 00:09:02,400 --> 00:09:06,000 it will simply skip this line, simply 'except' block, it will 171 00:09:06,000 --> 00:09:07,600 simply skip 'except' block. 172 00:09:09,100 --> 00:09:12,800 If something goes wrong here while running your codes, then 173 00:09:12,800 --> 00:09:16,500 your Python will stop this part and then it will try to execute 174 00:09:16,500 --> 00:09:19,300 the code which is there in your 'except' block. 175 00:09:20,600 --> 00:09:23,900 So it's like a simple thing like if and else but you know 176 00:09:23,900 --> 00:09:27,000 in if and else what will happen? If 'if' condition is false, then 177 00:09:27,000 --> 00:09:30,000 only 'else' will execute but here not like that. 178 00:09:30,300 --> 00:09:34,300 Your Python will try to execute this block first. In this 179 00:09:34,300 --> 00:09:35,300 maybe at any line 180 00:09:35,800 --> 00:09:39,900 if something goes wrong, then your Python will stop the execution 181 00:09:39,900 --> 00:09:41,700 of this block and immediately 182 00:09:41,700 --> 00:09:44,300 it will try to execute the block which is there under your 183 00:09:44,300 --> 00:09:45,600 'except'. That's it. 184 00:09:45,600 --> 00:09:48,700 [no audio] 185 00:09:48,700 --> 00:09:51,100 Right. See now, what I am doing is I don't know 186 00:09:51,100 --> 00:09:52,400 what is the error I'm getting as of now. 187 00:09:52,400 --> 00:09:55,300 That's why you can also get what type of error you are going 188 00:09:55,300 --> 00:09:58,700 to get here 'except Exception', 189 00:09:58,700 --> 00:09:59,700 what is the exception? 190 00:09:59,700 --> 00:10:02,100 [no audio] 191 00:10:02,100 --> 00:10:06,700 as some error 'e', and you can print that 192 00:10:06,700 --> 00:10:10,200 [no audio] 193 00:10:10,200 --> 00:10:12,900 because of this error you are stopping, your Python is going 194 00:10:12,900 --> 00:10:17,400 to stop your code. See that, 'No such file or directory 195 00:10:17,400 --> 00:10:18,600 called 'nari.txt'. 196 00:10:20,300 --> 00:10:23,300 That's it. Actually this is a 'FileNotFoundError'. 197 00:10:23,300 --> 00:10:25,500 [no audio] 198 00:10:25,500 --> 00:10:29,800 If you don't handle with the 'try' and 'except' then see instead of 199 00:10:29,800 --> 00:10:32,300 [no audio] 200 00:10:32,300 --> 00:10:35,100 in a smooth way you are handling your code, 201 00:10:35,300 --> 00:10:38,100 if you use 'try' and 'except'. If you don't use that what will happen? 202 00:10:38,100 --> 00:10:40,300 [no audio] 203 00:10:40,300 --> 00:10:42,600 See that. Simply I'm doing this, no. 204 00:10:42,600 --> 00:10:44,900 [no audio] 205 00:10:44,900 --> 00:10:47,000 You're getting like 'FileNotFoundError: 206 00:10:47,000 --> 00:10:48,400 No such file or directory'. 207 00:10:48,500 --> 00:10:49,800 Of course, the error is fine, 208 00:10:49,800 --> 00:10:54,300 but you are not handling properly to run your code. 209 00:10:54,700 --> 00:10:58,900 That's the reason whenever there is a chance to get an exception 210 00:10:59,400 --> 00:11:01,300 while running your code, at that time 211 00:11:01,300 --> 00:11:04,200 please try to use 'try' and 'except' block. 212 00:11:05,500 --> 00:11:09,400 And, these are simple thing guys, 'try' and 'except' block. 213 00:11:09,400 --> 00:11:11,900 So let me write how to handle 214 00:11:11,900 --> 00:11:14,400 [no audio] 215 00:11:14,400 --> 00:11:20,500 exceptions. Simple, it is an interview question, guys. With the 'try' 216 00:11:20,500 --> 00:11:24,400 and 'except' block. That's it. 217 00:11:24,400 --> 00:11:27,200 [no audio] 218 00:11:27,200 --> 00:11:29,200 Right. Then, what is the syntax for that? 219 00:11:30,300 --> 00:11:31,300 Very simple. 220 00:11:31,800 --> 00:11:35,200 'try:', whatever the code you have just try to write in a 'try:' 221 00:11:35,200 --> 00:11:42,200 block. Let me write some line number 1 or statement 1. You 222 00:11:42,200 --> 00:11:44,600 can write any number of statements under this 'try:'. 223 00:11:45,600 --> 00:11:47,900 'statement-3', something like that. 224 00:11:48,200 --> 00:11:51,300 If something goes wrong with any one of the statement which 225 00:11:51,300 --> 00:11:56,000 are there under your 'try' block then your Python will 226 00:11:56,000 --> 00:12:01,500 automatically skip the 'try' block and then it will try to execute 227 00:12:01,600 --> 00:12:04,500 'except:' block. In 'except:' block you can write something. 228 00:12:04,900 --> 00:12:06,100 So, "This is 229 00:12:06,100 --> 00:12:09,200 [no audio] 230 00:12:09,200 --> 00:12:11,400 because of something 231 00:12:13,800 --> 00:12:14,800 went wrong 232 00:12:14,800 --> 00:12:18,000 [no audio] 233 00:12:18,000 --> 00:12:19,800 in try block". 234 00:12:20,300 --> 00:12:25,700 So what is that? This is just to tell that something goes 235 00:12:25,700 --> 00:12:26,900 wrong in your 'try' block. 236 00:12:27,900 --> 00:12:33,600 But what I want is I want to see exactly what is the error 237 00:12:33,600 --> 00:12:37,900 is there, then what is the syntax we have to use 238 00:12:37,900 --> 00:12:43,300 is, same syntax. Actually now, this is useful guys. 239 00:12:45,000 --> 00:12:46,800 This is useful in your real time. 240 00:12:46,800 --> 00:12:48,800 [no audio] 241 00:12:48,800 --> 00:12:51,900 But this is simple way. We will see advanced level as well. 242 00:12:52,200 --> 00:12:57,900 'except Exception as e'. So why I am taking 'e', you can take 243 00:12:57,900 --> 00:12:59,400 anything, but this is an error 244 00:12:59,400 --> 00:13:02,100 that's why just to represent that I am taking 'e' variable. 245 00:13:03,000 --> 00:13:04,800 Right. Now, instead of this message 246 00:13:04,800 --> 00:13:06,100 what I can write is 247 00:13:08,200 --> 00:13:12,000 See, "This is because of", "This is", 248 00:13:12,000 --> 00:13:13,400 Now you know what is the error? 249 00:13:13,400 --> 00:13:16,100 I mean your Python knows that. Your Python handles that with 250 00:13:16,100 --> 00:13:19,900 variable called 'e' with the help of 'Exception' clause here. 251 00:13:20,900 --> 00:13:26,000 "This is because of", right, so you can write simply this way. That's it. 252 00:13:26,000 --> 00:13:30,200 [no audio] 253 00:13:30,200 --> 00:13:32,200 Right. Okay. 254 00:13:32,200 --> 00:13:35,200 [no audio] 255 00:13:35,200 --> 00:13:36,800 Now, let me show you one more thing. 256 00:13:38,400 --> 00:13:40,400 I am going to comment entire code. 257 00:13:40,400 --> 00:13:47,600 [no audio] 258 00:13:47,600 --> 00:13:48,900 See, what I am doing is 259 00:13:50,000 --> 00:13:51,600 I have a list called 260 00:13:53,000 --> 00:13:55,300 'my_list=' 261 00:13:55,300 --> 00:13:59,700 some values [3, 4, 5], and guys if I try to print 'my_list 262 00:14:01,300 --> 00:14:05,300 [1], or maybe 2, whatever it may be, you're going to get 263 00:14:05,300 --> 00:14:06,100 your output, right? 264 00:14:06,200 --> 00:14:07,700 'my_list[1]' is nothing but 4. 265 00:14:09,300 --> 00:14:15,000 But if I try to print 'my_list[4]', what is the result you are getting? 266 00:14:16,200 --> 00:14:17,800 As per syntax it is correct, 267 00:14:18,100 --> 00:14:22,300 but 'my_list[4]' is nothing but 5th value from your list. 268 00:14:23,000 --> 00:14:25,000 So we don't have five values in our list. 269 00:14:25,000 --> 00:14:29,900 How come this fourth value, fourth index value we can get? 270 00:14:30,500 --> 00:14:31,600 It's not possible, right? 271 00:14:32,100 --> 00:14:35,800 That's why what are you showing? 'IndexError: list index out of range'. 272 00:14:35,800 --> 00:14:37,600 [no audio] 273 00:14:37,600 --> 00:14:39,500 Right. See, what I will do is, 274 00:14:40,900 --> 00:14:47,100 I will try to print 'my_list' index of 4, in case if, I don't 275 00:14:47,100 --> 00:14:50,500 know how many values are there in 'my_list', just assume that. Somewhere 276 00:14:50,500 --> 00:14:53,000 'my_list' is there. I don't know. While writing our course you 277 00:14:53,000 --> 00:14:55,700 will come to know how many values. Sometimes you need to create 278 00:14:55,700 --> 00:14:58,000 dynamically your list based on some values. 279 00:14:58,900 --> 00:15:00,300 You don't know how many values are there. 280 00:15:00,400 --> 00:15:03,400 Logically, you need to get suppose some 5th value then 281 00:15:03,400 --> 00:15:06,000 what I will do is, I will try to print 5th value in this way, 282 00:15:06,700 --> 00:15:10,200 but there is a chance that is, you don't have any values inside 283 00:15:10,200 --> 00:15:13,300 of your list. Maybe you have only two values but you are trying 284 00:15:13,300 --> 00:15:14,500 to access 5th value. 285 00:15:15,000 --> 00:15:16,400 That's why what I am doing is 286 00:15:19,800 --> 00:15:23,300 'except Exception as e', then 287 00:15:23,300 --> 00:15:26,800 I am trying to print that 'e'. So that instead of getting an 288 00:15:26,800 --> 00:15:29,600 error, I can see some message. Of course, 289 00:15:30,000 --> 00:15:32,800 if you get an error you can, you're also going to see message, 290 00:15:32,800 --> 00:15:35,300 but the problem is you're going to terminate your script. 291 00:15:36,600 --> 00:15:39,000 But with exception handling, you're not terminating your script. 292 00:15:39,000 --> 00:15:41,500 See that, after 'try' and 'except', I am writing 293 00:15:42,700 --> 00:15:46,400 "This code will also execute". 294 00:15:46,400 --> 00:15:49,400 [no audio] 295 00:15:49,400 --> 00:15:53,100 Now see the result. You are getting an error like 'list index out of range'. 296 00:15:53,100 --> 00:15:56,000 This code will also execute. But instead of that, suppose 297 00:15:56,000 --> 00:15:59,100 if I don't have 'try' and 'except' block, and if I try to print 298 00:15:59,100 --> 00:16:04,100 directly from our list a value called 5th value see that what 299 00:16:04,100 --> 00:16:08,500 will happen. 'print(my_list[4])' I'm trying to print. See 300 00:16:08,500 --> 00:16:10,800 [no audio] 301 00:16:10,800 --> 00:16:13,000 index, 'list index out of range', and your Python 302 00:16:13,000 --> 00:16:14,800 program is terminating here itself. 303 00:16:15,600 --> 00:16:17,100 It is not trying to execute this one. 304 00:16:17,100 --> 00:16:19,600 [no audio] 305 00:16:19,600 --> 00:16:21,400 See actually do we have any dependency 306 00:16:21,400 --> 00:16:23,100 between this line and this line? 307 00:16:23,100 --> 00:16:26,200 No. Because here you are using list, but nowhere we are 308 00:16:26,200 --> 00:16:27,900 using list in 39th line. 309 00:16:28,700 --> 00:16:30,500 If you are not using then no problem, 310 00:16:30,500 --> 00:16:31,700 you can execute this line, no. 311 00:16:31,900 --> 00:16:33,400 Why should we stop your code? 312 00:16:34,500 --> 00:16:38,600 That's why, that's the reason we are using 'try' and 'except' 313 00:16:38,600 --> 00:16:40,000 block in your code. 314 00:16:40,000 --> 00:16:42,900 [no audio] 315 00:16:42,900 --> 00:16:45,700 Right. Fine. So simply. 316 00:16:48,200 --> 00:16:49,200 Yeah, see here. 317 00:16:49,200 --> 00:16:51,000 [no audio] 318 00:16:51,000 --> 00:16:52,100 This is the syntax, right. 319 00:16:52,700 --> 00:16:56,300 So 'try:', whatever the main code you have to run that code 320 00:16:56,300 --> 00:16:59,000 you just try to write under 'try:' block. 321 00:16:59,000 --> 00:17:00,900 [no audio] 322 00:17:00,900 --> 00:17:05,000 So in case if something goes wrong while running your code, 323 00:17:05,098 --> 00:17:09,598 then what you want to display, then that you have to write 324 00:17:09,500 --> 00:17:10,800 under 'except:' block. 325 00:17:12,500 --> 00:17:16,700 Right. So this is just simple thing to handle your 'try' and 'except', 326 00:17:16,700 --> 00:17:20,500 I mean to handle your exceptions, but you need to use 'try' 327 00:17:20,500 --> 00:17:26,500 and 'except' as 'except Exception as e' then try to print that 328 00:17:26,500 --> 00:17:27,800 error. This is useful. 329 00:17:27,800 --> 00:17:31,200 [no audio] 330 00:17:31,200 --> 00:17:32,200 Right. 331 00:17:37,099 --> 00:17:38,500 This one is important one. 332 00:17:39,400 --> 00:17:40,400 That's fine. 333 00:17:41,700 --> 00:17:47,800 Now, let me show you some of the examples where you are going 334 00:17:47,800 --> 00:17:50,300 to get exceptions. 'IndexError', 335 00:17:50,300 --> 00:17:52,099 we have seen that. 'ZeroDivisionError', 336 00:17:52,099 --> 00:17:55,700 we have seen that. 'ImportError', 'ValueError'. 337 00:17:55,700 --> 00:17:57,500 What is 'ImportError' means, just assume that you're trying 338 00:17:57,500 --> 00:17:58,600 to import some module, 339 00:17:59,100 --> 00:18:01,600 so if that module is already there with your Python as a 340 00:18:01,600 --> 00:18:06,100 default module, then you won't get any exceptions but assume 341 00:18:06,100 --> 00:18:11,000 that you want to read Excel sheets, to read Excel sheets suppose 342 00:18:11,000 --> 00:18:13,000 you need to use 'xlrd' module. 343 00:18:13,200 --> 00:18:15,800 This is not the default module with your Python, then 344 00:18:17,200 --> 00:18:21,800 while importing this module you will get an error, right? 345 00:18:22,800 --> 00:18:26,100 So whenever you are trying to import third-party modules, 346 00:18:26,100 --> 00:18:28,100 you need to use 'try' and 'except' block. 347 00:18:28,100 --> 00:18:29,300 Let me show you that as well. 348 00:18:30,100 --> 00:18:31,100 It's important one. 349 00:18:32,000 --> 00:18:36,200 So what I am doing is, let me comment once again your code. 350 00:18:36,200 --> 00:18:40,800 [no audio] 351 00:18:40,800 --> 00:18:46,300 Just assume that I need to import a module called, suppose 352 00:18:46,300 --> 00:18:47,900 some 'fabric'. 353 00:18:47,900 --> 00:18:49,700 [no audio] 354 00:18:49,700 --> 00:18:50,700 Let me run it. 355 00:18:52,200 --> 00:18:53,800 There is no module, right? 356 00:18:53,800 --> 00:18:57,100 No module named 'fabric'. 'ModuleNotFoundError'. 357 00:18:57,100 --> 00:18:59,400 [no audio] 358 00:18:59,400 --> 00:19:02,100 Right. See that. What I can do is in that case 'try:' 359 00:19:05,400 --> 00:19:11,500 'except Exception as e', then I am printing simply error. 360 00:19:11,900 --> 00:19:14,200 So instead of terminating by your Python 361 00:19:14,900 --> 00:19:19,700 we are terminating, we are stopping or skipping gracefully, 362 00:19:20,200 --> 00:19:26,600 right. So that is the way to use your 'try' and 'except' block. That's it. 363 00:19:27,600 --> 00:19:31,700 So I am giving just some of the errors - 'TypeError', 'NameError', 364 00:19:31,700 --> 00:19:34,600 'FileNotFoundError', something like that. Okay. 365 00:19:34,800 --> 00:19:37,200 So while going forward we will see different types of errors 366 00:19:37,200 --> 00:19:42,300 and how to handle them. And we have one more video on this just 367 00:19:42,300 --> 00:19:47,500 to handle directly the known errors, right? Okay, guys. 368 00:19:48,100 --> 00:19:49,400 Yeah, one more thing, 'IOError'. 369 00:19:49,400 --> 00:19:52,100 We will see them, all of them while going forward. 370 00:19:52,600 --> 00:19:54,900 Okay, don't miss next video, okay. 371 00:19:55,400 --> 00:19:57,300 Okay guys, thank you for watching this video. 372 00:19:57,300 --> 00:20:03,000 [no audio]