1 00:00:00,000 --> 00:00:02,100 [no audio] 2 00:00:02,100 --> 00:00:03,800 Friends here we are going to 3 00:00:03,800 --> 00:00:07,400 see the difference between 'try except finally' 4 00:00:07,400 --> 00:00:10,100 and 'try except else' block. See already 5 00:00:10,100 --> 00:00:16,200 we know simple 'try' and 'except'; and 'try except Exception as e:', 6 00:00:16,200 --> 00:00:21,900 and also by adding known exceptions, right. 7 00:00:22,300 --> 00:00:27,500 See I mean, let me open my editor and I am going to save 8 00:00:27,500 --> 00:00:30,800 it as a simple script 'differences'. 9 00:00:31,800 --> 00:00:33,100 Yeah, I will take this one. 10 00:00:33,100 --> 00:00:36,500 [no audio] 11 00:00:36,500 --> 00:00:40,300 Now see that. So already we know if I try to print something 12 00:00:40,300 --> 00:00:43,200 called variable 'a', right, 13 00:00:43,200 --> 00:00:45,900 [no audio] 14 00:00:45,900 --> 00:00:47,100 Simply I am printing, 15 00:00:47,100 --> 00:00:51,100 [no audio] 16 00:00:51,100 --> 00:00:53,300 "Exception occured". 17 00:00:54,100 --> 00:00:58,200 So guys, I am trying to print variable value 'a' but if that 18 00:00:58,200 --> 00:01:00,300 is not defined definitely you'll get an exception. 19 00:01:01,500 --> 00:01:03,700 That's why I'm handling in this way, instead of getting an 20 00:01:03,700 --> 00:01:06,000 error. It's correct. 21 00:01:06,900 --> 00:01:10,800 But the thing is what type of exception you are getting. 22 00:01:11,300 --> 00:01:17,600 So for that what you can do, you can add 'Exception as e' statement, 23 00:01:18,000 --> 00:01:21,200 and then you can print that error as well. 24 00:01:22,700 --> 00:01:28,200 Yes, I'm getting that. But if name is not defined, you know 25 00:01:28,200 --> 00:01:31,700 that what is the exception your Python is going to raise, that 26 00:01:31,700 --> 00:01:33,200 is 'NameError'. 27 00:01:33,700 --> 00:01:37,200 That's why I can handle known errors in this way 28 00:01:37,200 --> 00:01:40,700 also. Before your 'except Exception as e' block 29 00:01:41,100 --> 00:01:46,500 you can write in this way. Likewise any, right, any exception 30 00:01:46,500 --> 00:01:48,400 you can handle if you know the exceptions. 31 00:01:50,000 --> 00:01:53,000 "Variable is not defined". Now, which block is going to execute? 32 00:01:53,000 --> 00:01:55,200 [no audio] 33 00:01:55,200 --> 00:01:59,100 Right. This block is going to execute. Suppose if 'a' is defined 34 00:01:59,100 --> 00:02:00,600 then there is no exception, 35 00:02:00,600 --> 00:02:04,100 right? If 'a' is defined, no exception. Simply you are printing 36 00:02:04,100 --> 00:02:04,900 'a' variable value. 37 00:02:05,200 --> 00:02:06,600 It may be any value, right? 38 00:02:06,600 --> 00:02:08,600 [no audio] 39 00:02:08,600 --> 00:02:09,600 That's fine. 40 00:02:10,300 --> 00:02:13,000 But if 'a' is not defined then only your Python is going to 41 00:02:13,000 --> 00:02:18,900 raise an exception, and if that exception is there here, then 42 00:02:18,900 --> 00:02:21,600 it will match and it will execute that 'except' block. 43 00:02:22,800 --> 00:02:23,800 That's fine. 44 00:02:24,500 --> 00:02:27,300 Likewise you can handle any number of exceptions if you know, 45 00:02:28,200 --> 00:02:30,100 and we know we have 'finally' as well. 46 00:02:30,100 --> 00:02:32,100 What is the usage of 'finally'? 47 00:02:33,400 --> 00:02:35,900 "This will execute 48 00:02:35,900 --> 00:02:42,200 [no audio] 49 00:02:42,200 --> 00:02:43,200 always". 50 00:02:44,700 --> 00:02:45,700 Let me run it. 51 00:02:45,700 --> 00:02:47,900 [no audio] 52 00:02:47,900 --> 00:02:52,300 See as of now 'a' is 10 and you're trying to print 'a' variable 53 00:02:52,300 --> 00:02:54,200 value, so there is no exception. 54 00:02:54,600 --> 00:02:58,900 So your 'try' block is success. If 'try' block is success, you're 55 00:02:58,900 --> 00:03:01,100 not getting an exception. That's fine. 56 00:03:01,600 --> 00:03:05,400 But if 'finally' is there, whether you are going to execute your 57 00:03:05,400 --> 00:03:08,600 'try' block successfully or not, or whether you are going to 58 00:03:08,600 --> 00:03:10,100 raise any exception or not, 59 00:03:10,900 --> 00:03:12,700 'finally' will always execute. 60 00:03:13,000 --> 00:03:16,200 So whatever the code is there in 'finally' that will always 61 00:03:16,200 --> 00:03:18,600 execute. That's fine. 62 00:03:19,300 --> 00:03:21,300 Now, what I am doing is, let me comment this. 63 00:03:21,300 --> 00:03:26,300 [no audio] 64 00:03:26,300 --> 00:03:27,900 Now I am going to write 'else' block. 65 00:03:28,500 --> 00:03:29,900 So I am writing simply 'print( 66 00:03:29,900 --> 00:03:32,300 [no audio] 67 00:03:32,300 --> 00:03:33,900 "This will execute 68 00:03:33,900 --> 00:03:36,100 [no audio] 69 00:03:36,100 --> 00:03:41,100 if there is no, if there is no exceptions 70 00:03:41,100 --> 00:03:43,900 [no audio] 71 00:03:43,900 --> 00:03:45,600 in try block". That's it. 72 00:03:45,600 --> 00:03:48,300 Simple. 'finally' is always executing, 73 00:03:48,300 --> 00:03:52,600 but whereas if you add 'else' block after all your exceptions 74 00:03:52,600 --> 00:03:55,300 in your 'try' block what will happen means, 75 00:03:57,000 --> 00:04:00,900 if there is no exception, if everything goes good with your 76 00:04:00,900 --> 00:04:05,700 'try' block then only 'else' will also execute. See as of now, 77 00:04:05,900 --> 00:04:06,800 everything is good. 78 00:04:06,900 --> 00:04:09,800 I mean 'try' block is good, right. There is no chance to 79 00:04:09,800 --> 00:04:12,000 get any exception because 'a' is there and you're trying 80 00:04:12,000 --> 00:04:13,300 to print 'a' variable value. 81 00:04:13,300 --> 00:04:15,600 [no audio] 82 00:04:15,600 --> 00:04:17,899 Now this will execute if there is no exception 83 00:04:17,899 --> 00:04:19,100 in your 'try' block. 84 00:04:19,100 --> 00:04:21,300 Now, what I am doing is, I am deleting 'a' variable value. 85 00:04:21,399 --> 00:04:24,899 Now, you are going to get an exception "a is not defined", that 86 00:04:24,899 --> 00:04:27,100 is 'NameError', "Variable is not defined". 87 00:04:27,700 --> 00:04:32,400 So if you get any exception in your 'try' block, then your 88 00:04:32,400 --> 00:04:34,500 'else' won't execute. Now see the result. 89 00:04:36,100 --> 00:04:38,800 You're not getting this message, 'else' block message. 90 00:04:39,500 --> 00:04:44,800 So simply if this is success, 91 00:04:46,300 --> 00:04:52,700 right, then only 'else' will execute. If 'try' will give 92 00:04:52,700 --> 00:04:56,500 you any, suppose if any exception occurred in 'try' then 'else' 93 00:04:56,500 --> 00:04:57,600 won't execute. 94 00:04:58,700 --> 00:05:00,000 Right. Fine. 95 00:05:00,800 --> 00:05:04,500 So, you know the difference 'else' and then 'finally'. And guys, 96 00:05:04,500 --> 00:05:08,900 you can also include after 'else', 'finally' also. That means 97 00:05:08,900 --> 00:05:13,400 actually 'try', 'except', any number of 'exception' blocks, then 'else' 98 00:05:13,400 --> 00:05:18,600 and 'finally' is the complete syntax if you want. So 'finally' 99 00:05:18,600 --> 00:05:21,900 and 'else' are optional. These two are optional, if you want 100 00:05:21,900 --> 00:05:22,800 you can include it. 101 00:05:23,800 --> 00:05:25,400 Right. Now see I'm running. 102 00:05:25,400 --> 00:05:27,200 [no audio] 103 00:05:27,200 --> 00:05:27,800 What will happen? 104 00:05:27,800 --> 00:05:29,600 [no audio] 105 00:05:29,600 --> 00:05:32,800 'print(a)'. Some exception occurred, that is 'NameError'. 106 00:05:33,100 --> 00:05:35,600 So that's why "Variable is not defined", you are printing. So 107 00:05:35,600 --> 00:05:38,700 if exception occurred then anyway 'else' won't execute. 108 00:05:38,800 --> 00:05:41,800 So 'else' message you are not getting, but 'finally' you are getting 109 00:05:42,100 --> 00:05:44,200 because 'finally' will always will execute that. 110 00:05:45,600 --> 00:05:49,400 Now I am defining 'a' variable value. Now 111 00:05:49,400 --> 00:05:51,600 I'm running. See the result. 112 00:05:52,200 --> 00:05:54,500 So there is no exception in your 'try' block, 113 00:05:54,700 --> 00:05:59,400 that's why it is successful. Your 'try' is success. If 'try' is success 114 00:05:59,500 --> 00:06:00,600 'else' will execute. 115 00:06:01,200 --> 00:06:03,200 Yes, you are getting. Anyway, 'finally', 116 00:06:03,200 --> 00:06:04,400 'finally' will always execute. 117 00:06:04,400 --> 00:06:05,300 That's what it is doing. 118 00:06:06,500 --> 00:06:11,900 So this is the complete syntax for your 'try, except, and else, 119 00:06:12,200 --> 00:06:14,900 finally' block, right? 120 00:06:15,300 --> 00:06:18,800 So guys for time being try to remember the syntax if you 121 00:06:18,800 --> 00:06:21,400 write in this way what will happen. At what time, at what 122 00:06:21,400 --> 00:06:23,400 block is going to execute, right. 123 00:06:23,600 --> 00:06:26,700 So once we start with real-time scenarios there I will try 124 00:06:26,700 --> 00:06:31,000 to use this effectively, up to that I can't use it. For time being 125 00:06:31,000 --> 00:06:32,500 try to remember these syntaxes. 126 00:06:33,900 --> 00:06:35,400 Okay. Okay guys, 127 00:06:35,400 --> 00:06:36,800 Thank you for watching this video. 128 00:06:36,800 --> 00:06:44,513 [no audio]