1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue our discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And in this video we are going to discuss about exception handling.

4
00:00:06,000 --> 00:00:06,000
Right.

5
00:00:06,000 --> 00:00:09,000
And I hope you have heard about this particular term.

6
00:00:09,000 --> 00:00:11,000
And uh, what exactly is an exception handling?

7
00:00:11,000 --> 00:00:17,000
Exception handling in Python allows you to handle errors gracefully and take corrective actions without

8
00:00:17,000 --> 00:00:19,000
stopping the execution of the program.

9
00:00:19,000 --> 00:00:20,000
Right.

10
00:00:20,000 --> 00:00:25,000
And uh, here in this specific lesson, we are going to cover, uh, and we'll be seeing some of the

11
00:00:25,000 --> 00:00:28,000
keywords like try except else and finally blocks.

12
00:00:28,000 --> 00:00:33,000
And we will be seeing how we can handle the exception that arise in our program.

13
00:00:33,000 --> 00:00:37,000
And we have to handle in such a way that we do not break the entire execution of the program.

14
00:00:37,000 --> 00:00:38,000
Right now.

15
00:00:38,000 --> 00:00:39,000
There may be scenarios.

16
00:00:39,000 --> 00:00:43,000
Let's say that I have I'm using an application over there.

17
00:00:43,000 --> 00:00:47,000
A user is filling some kind of form and it does not give the right kind of input.

18
00:00:47,000 --> 00:00:50,000
So in that particular case, I will not just stop the execution of the program.

19
00:00:50,000 --> 00:00:53,000
Instead I will say, hey, I will raise an exception.

20
00:00:53,000 --> 00:00:57,000
I'll tell the user, hey, try to put some proper information over there, right?

21
00:00:57,000 --> 00:01:02,000
And make sure that you always try to put this kind of information to make that entire form get submitted.

22
00:01:02,000 --> 00:01:03,000
Okay.

23
00:01:03,000 --> 00:01:04,000
So we are handling it.

24
00:01:04,000 --> 00:01:06,000
We are handling it gracefully.

25
00:01:06,000 --> 00:01:06,000
Right?

26
00:01:06,000 --> 00:01:08,000
We are not stopping the execution of the program.

27
00:01:08,000 --> 00:01:11,000
Now, let us go ahead and understand what are the exceptions.

28
00:01:11,000 --> 00:01:14,000
And you really need to understand the differences between errors and exception.

29
00:01:15,000 --> 00:01:19,000
Exceptions are events that disrupt the normal flow of a program.

30
00:01:19,000 --> 00:01:24,000
They occur where an error is encountered during program execution.

31
00:01:24,000 --> 00:01:26,000
Common exceptions include zero division error.

32
00:01:26,000 --> 00:01:31,000
So here we have something like divide by zero file not found or value error type error.

33
00:01:31,000 --> 00:01:32,000
These are some of the examples.

34
00:01:32,000 --> 00:01:33,000
Okay.

35
00:01:33,000 --> 00:01:38,000
Now let me just show you uh like uh uh I'll just show you some amazing examples.

36
00:01:38,000 --> 00:01:40,000
And how do I handle it?

37
00:01:40,000 --> 00:01:42,000
With the help of exception handling, I will try to talk about it.

38
00:01:42,000 --> 00:01:44,000
Okay, now let me do one thing.

39
00:01:44,000 --> 00:01:44,000
Okay.

40
00:01:44,000 --> 00:01:47,000
Let me just go ahead and write a is equal to ten okay.

41
00:01:47,000 --> 00:01:51,000
So this code I think it will work perfectly because we have assigned the variable.

42
00:01:51,000 --> 00:01:55,000
But if I go ahead and write e is equal to b and if I just execute it.

43
00:01:55,000 --> 00:01:57,000
So what is the error that I'm getting over here.

44
00:01:57,000 --> 00:01:59,000
Write error is very simple.

45
00:01:59,000 --> 00:02:02,000
It is saying hey name B is not defined.

46
00:02:02,000 --> 00:02:03,000
So what has happened over here?

47
00:02:03,000 --> 00:02:06,000
This is a kind of error that has actually happened.

48
00:02:06,000 --> 00:02:12,000
And as I have already told, they occur when an error is encountered during a program execution here.

49
00:02:12,000 --> 00:02:16,000
Obviously I know the error is like name B is not defined.

50
00:02:16,000 --> 00:02:19,000
So an exception is basically getting raised over here.

51
00:02:19,000 --> 00:02:23,000
And this name error is one kind of exception, right?

52
00:02:23,000 --> 00:02:25,000
It is one kind of exception like how I have written over here.

53
00:02:25,000 --> 00:02:28,000
Zero division error file not found error value error type error.

54
00:02:28,000 --> 00:02:33,000
So name error is also one kind of exception that is probably getting raised over here because we have

55
00:02:33,000 --> 00:02:35,000
not defined B okay.

56
00:02:35,000 --> 00:02:37,000
So how do I handle this.

57
00:02:37,000 --> 00:02:39,000
Let's say that I'm not defined B over here.

58
00:02:39,000 --> 00:02:45,000
And obviously this kind of error that I probably see it is suitable for a coder.

59
00:02:45,000 --> 00:02:45,000
Right?

60
00:02:45,000 --> 00:02:47,000
A coder will be able to understand.

61
00:02:47,000 --> 00:02:47,000
Okay, fine.

62
00:02:47,000 --> 00:02:50,000
In this particular line of code I'm getting this kind of exception.

63
00:02:50,000 --> 00:02:51,000
Right.

64
00:02:51,000 --> 00:02:57,000
But if a user if a user is using some kind of application, if he or she sees this right, they will

65
00:02:57,000 --> 00:02:58,000
not be able to understand.

66
00:02:58,000 --> 00:03:02,000
So it is always better that we handle it gracefully by providing a good message.

67
00:03:02,000 --> 00:03:03,000
Right?

68
00:03:03,000 --> 00:03:04,000
So how do I handle this?

69
00:03:04,000 --> 00:03:09,000
So what I will do in order to handle exception handling right.

70
00:03:09,000 --> 00:03:11,000
Let me just go ahead and write.

71
00:03:11,000 --> 00:03:16,000
I'll be using over here try accept except.

72
00:03:17,000 --> 00:03:19,000
Try except blocks.

73
00:03:19,000 --> 00:03:19,000
Okay.

74
00:03:19,000 --> 00:03:22,000
It is just like try catch block in other programming language.

75
00:03:22,000 --> 00:03:24,000
So first of all, I will go ahead and write.

76
00:03:24,000 --> 00:03:24,000
Try.

77
00:03:24,000 --> 00:03:25,000
Okay.

78
00:03:25,000 --> 00:03:26,000
Now what does try mean?

79
00:03:26,000 --> 00:03:34,000
Try basically means inside this block I will go ahead and write my block of code where exception may

80
00:03:34,000 --> 00:03:35,000
arise.

81
00:03:35,000 --> 00:03:35,000
Okay.

82
00:03:35,000 --> 00:03:38,000
So let's say right now the exception was rising.

83
00:03:38,000 --> 00:03:40,000
In this particular code write a is equal to B.

84
00:03:40,000 --> 00:03:45,000
Right now What I will do is that when I write a is equal to B, right?

85
00:03:45,000 --> 00:03:47,000
Obviously we know that an error will come over here.

86
00:03:47,000 --> 00:03:53,000
Now in order to handle this error, what I will do, I will just go ahead and write accept.

87
00:03:53,000 --> 00:03:54,000
Okay.

88
00:03:54,000 --> 00:03:57,000
And I will just go ahead and write my print statement.

89
00:03:57,000 --> 00:04:01,000
I'll say, hey, the variable has not been assigned right.

90
00:04:01,000 --> 00:04:04,000
The variable has not been assigned.

91
00:04:04,000 --> 00:04:04,000
Okay.

92
00:04:04,000 --> 00:04:10,000
So this is my custom message that I'm saying hey if this particular error see when this code gets run

93
00:04:10,000 --> 00:04:13,000
here exception got raised right.

94
00:04:13,000 --> 00:04:16,000
And this exception will be caught by this accept block.

95
00:04:16,000 --> 00:04:20,000
And when it will get caught here is the print statement.

96
00:04:20,000 --> 00:04:22,000
This is the print statement that will get displayed right.

97
00:04:22,000 --> 00:04:24,000
So if I go ahead and execute it once again.

98
00:04:24,000 --> 00:04:28,000
So here you'll be able to see hey see the variable has not been assigned.

99
00:04:28,000 --> 00:04:33,000
Now this is a very good message when compared to the previous message where we had read statements.

100
00:04:33,000 --> 00:04:34,000
And all for the end user.

101
00:04:34,000 --> 00:04:35,000
Right.

102
00:04:35,000 --> 00:04:39,000
And obviously we know that what is the kind of error that we have got over here.

103
00:04:39,000 --> 00:04:42,000
A is equal to b, c this is the error that we are getting.

104
00:04:42,000 --> 00:04:43,000
Name B is not defined.

105
00:04:43,000 --> 00:04:44,000
It is not like that.

106
00:04:44,000 --> 00:04:45,000
I cannot display this.

107
00:04:45,000 --> 00:04:46,000
I can also display this.

108
00:04:46,000 --> 00:04:48,000
Let me show you how we can display this.

109
00:04:48,000 --> 00:04:50,000
So what I will do I will just copy the same thing.

110
00:04:50,000 --> 00:04:52,000
I will paste it over here.

111
00:04:52,000 --> 00:04:56,000
I know what kind of error or what kind of exception it is throwing.

112
00:04:56,000 --> 00:04:58,000
It is a name error exception.

113
00:04:58,000 --> 00:05:01,000
So I will go ahead and call this same exception.

114
00:05:02,000 --> 00:05:02,000
Okay.

115
00:05:02,000 --> 00:05:04,000
This is one type of exception over here.

116
00:05:04,000 --> 00:05:04,000
Right.

117
00:05:04,000 --> 00:05:07,000
And let me just go ahead and make my alias right.

118
00:05:07,000 --> 00:05:10,000
And I will just go ahead and say hey print X okay.

119
00:05:10,000 --> 00:05:13,000
So what I'm actually doing when this kind of error is falling, right.

120
00:05:13,000 --> 00:05:17,000
When this kind of error is getting raised, a name error is getting raised.

121
00:05:17,000 --> 00:05:21,000
I am handling those kind of exception with the same exception handler over here.

122
00:05:21,000 --> 00:05:22,000
Right.

123
00:05:22,000 --> 00:05:26,000
And this name error is a class which handles this kind of exception.

124
00:05:26,000 --> 00:05:26,000
Right.

125
00:05:26,000 --> 00:05:28,000
I hope you are able to understand it.

126
00:05:28,000 --> 00:05:32,000
So now if I go ahead and execute it see the same message is getting displayed.

127
00:05:32,000 --> 00:05:33,000
Name B is not defined.

128
00:05:33,000 --> 00:05:35,000
And here also we are getting name B is not defined.

129
00:05:35,000 --> 00:05:36,000
Why?

130
00:05:36,000 --> 00:05:39,000
Because I have used the same class right.

131
00:05:39,000 --> 00:05:43,000
That is name error which is responsible in catching this specific exception.

132
00:05:44,000 --> 00:05:44,000
Right.

133
00:05:44,000 --> 00:05:48,000
So this is one amazing example which you can easily understand right now.

134
00:05:48,000 --> 00:05:51,000
Similarly I may have other kind of exception right.

135
00:05:51,000 --> 00:05:52,000
So let's go ahead and do this.

136
00:05:52,000 --> 00:05:56,000
I will go ahead and write hey let's do this okay I will go ahead and write.

137
00:05:56,000 --> 00:05:58,000
Result is equal to one divided by zero.

138
00:05:58,000 --> 00:06:01,000
If I execute this you see what kind of error is coming right.

139
00:06:01,000 --> 00:06:04,000
This is called as zero division error right.

140
00:06:04,000 --> 00:06:05,000
Zero division error.

141
00:06:05,000 --> 00:06:10,000
Now zero division error is again a kind of error that we are getting at.

142
00:06:10,000 --> 00:06:12,000
And this is also kind of an exception.

143
00:06:12,000 --> 00:06:12,000
Right.

144
00:06:12,000 --> 00:06:14,000
And we can also handle this exception.

145
00:06:14,000 --> 00:06:15,000
How do we handle it.

146
00:06:15,000 --> 00:06:18,000
I will go ahead and write my try block okay.

147
00:06:18,000 --> 00:06:20,000
And here my code will be there.

148
00:06:20,000 --> 00:06:29,000
Now I know if this exception gets raised I'm going to basically called except uh, except and my exception

149
00:06:29,000 --> 00:06:34,000
handler will be or my class name that I have to use which will cast this kind of exception is zero division

150
00:06:34,000 --> 00:06:36,000
error as x.

151
00:06:36,000 --> 00:06:38,000
Okay, X is just an alias.

152
00:06:38,000 --> 00:06:40,000
And we will go ahead and print x.

153
00:06:40,000 --> 00:06:41,000
Okay.

154
00:06:41,000 --> 00:06:44,000
Now you can see division by zero right.

155
00:06:44,000 --> 00:06:49,000
And apart from this I can also go ahead and write another message saying that hey please enter a number.

156
00:06:49,000 --> 00:06:52,000
Please enter the denominator.

157
00:06:52,000 --> 00:06:55,000
Denominator greater than zero.

158
00:06:55,000 --> 00:06:56,000
Right.

159
00:06:56,000 --> 00:06:57,000
So I can go ahead and write this.

160
00:06:57,000 --> 00:07:01,000
So this way I am able to get more beautiful messages.

161
00:07:01,000 --> 00:07:03,000
And I'm handing handling it gracefully.

162
00:07:03,000 --> 00:07:04,000
You know what I say.

163
00:07:04,000 --> 00:07:06,000
What do I mean by gracefully?

164
00:07:06,000 --> 00:07:11,000
I'm not stopping the execution of the program, not giving that entire read section that you always

165
00:07:11,000 --> 00:07:13,000
be, always are having a fear right?

166
00:07:13,000 --> 00:07:14,000
Of that.

167
00:07:14,000 --> 00:07:18,000
I'm giving a proper message over here saying that, hey, this is the exception that got raised.

168
00:07:18,000 --> 00:07:19,000
You need to fix this.

169
00:07:19,000 --> 00:07:21,000
Please enter the denominator greater than zero.

170
00:07:21,000 --> 00:07:25,000
So again, the person will go ahead and make sure that he'll understand.

171
00:07:25,000 --> 00:07:28,000
And then they'll go and write this particular denominator greater than zero.

172
00:07:28,000 --> 00:07:31,000
Right now all this exception one more things right.

173
00:07:31,000 --> 00:07:35,000
So here you can see that all these exceptions that are there like zero division error file not found

174
00:07:35,000 --> 00:07:37,000
error value error type error.

175
00:07:37,000 --> 00:07:45,000
These are derived from a main exception class Okay, so let's say that I don't know, there may be some

176
00:07:45,000 --> 00:07:47,000
kind of exception that may be happening.

177
00:07:47,000 --> 00:07:48,000
Okay.

178
00:07:48,000 --> 00:07:52,000
Um, let's say that over here again, if I go ahead and write a is equal to B, right.

179
00:07:52,000 --> 00:07:56,000
And let's say I am going to just write one by two.

180
00:07:56,000 --> 00:07:58,000
Now here you will be seeing that.

181
00:07:58,000 --> 00:07:59,000
Okay.

182
00:07:59,000 --> 00:08:01,000
Or let me just go ahead and write like this.

183
00:08:01,000 --> 00:08:02,000
I will copy this.

184
00:08:02,000 --> 00:08:06,000
Let's say I go ahead and copy this and I go ahead and write one by two.

185
00:08:06,000 --> 00:08:09,000
So obviously zero error is not going to get caught.

186
00:08:09,000 --> 00:08:09,000
Right.

187
00:08:09,000 --> 00:08:15,000
So now if I go ahead and write a is equal to B now I know A is equal to b is going to give me a name

188
00:08:15,000 --> 00:08:15,000
error.

189
00:08:15,000 --> 00:08:17,000
But will this exception get caught?

190
00:08:17,000 --> 00:08:24,000
Right now the answer is no because I have just written zero division error over here and I'm catching

191
00:08:24,000 --> 00:08:25,000
it in the accept block.

192
00:08:25,000 --> 00:08:28,000
Other than that, I have not used name error, right?

193
00:08:28,000 --> 00:08:32,000
This exception handle this exception is never handled it with the help of Nameerror.

194
00:08:32,000 --> 00:08:33,000
Right.

195
00:08:33,000 --> 00:08:35,000
So I have not written it in the except block, right?

196
00:08:35,000 --> 00:08:37,000
So that is the reason I'm getting this particular error.

197
00:08:37,000 --> 00:08:39,000
Saying that the name be is not defined.

198
00:08:39,000 --> 00:08:40,000
Right.

199
00:08:40,000 --> 00:08:41,000
So how do I handle it.

200
00:08:41,000 --> 00:08:47,000
So see one more thing is that and sometime it happens in such a way that they may be different different

201
00:08:47,000 --> 00:08:48,000
exception that may be occurring.

202
00:08:48,000 --> 00:08:49,000
Right.

203
00:08:49,000 --> 00:08:53,000
And all these exceptions that we learn like zero division error or name error value error.

204
00:08:53,000 --> 00:08:57,000
This is derived from one single parent class.

205
00:08:57,000 --> 00:08:57,000
Right.

206
00:08:57,000 --> 00:09:01,000
We'll talk about more classes and all that is nothing but exception.

207
00:09:01,000 --> 00:09:04,000
This is the base class of exception.

208
00:09:04,000 --> 00:09:06,000
It can handle any kind of exception.

209
00:09:06,000 --> 00:09:07,000
Right.

210
00:09:07,000 --> 00:09:10,000
So if I go ahead and write as x one okay.

211
00:09:10,000 --> 00:09:13,000
And now let me just go ahead and print x one.

212
00:09:14,000 --> 00:09:15,000
Now we'll see.

213
00:09:16,000 --> 00:09:21,000
Since I'm getting an error over here obviously you will be seeing that this exception is not getting

214
00:09:21,000 --> 00:09:23,000
handled over here because this is a zero division error.

215
00:09:24,000 --> 00:09:29,000
But this exception will probably handle because this is the main parent class of exception.

216
00:09:29,000 --> 00:09:33,000
And every exception will be derived from this particular exception class.

217
00:09:33,000 --> 00:09:34,000
Right.

218
00:09:34,000 --> 00:09:37,000
Like zero division error will also be derived from this name.

219
00:09:37,000 --> 00:09:39,000
Value error will also be derived from this.

220
00:09:39,000 --> 00:09:44,000
So at the end of the day you'll be seeing that this exception will be handled over here.

221
00:09:44,000 --> 00:09:45,000
Right.

222
00:09:45,000 --> 00:09:48,000
So if I go ahead and execute it once again.

223
00:09:48,000 --> 00:09:51,000
So here you can see name B is not defined from where it is basically coming.

224
00:09:51,000 --> 00:09:51,000
Right.

225
00:09:51,000 --> 00:09:53,000
So here it is basically coming from right.

226
00:09:53,000 --> 00:09:59,000
Main exception class main exception got caught here.

227
00:10:02,000 --> 00:10:02,000
Right.

228
00:10:02,000 --> 00:10:05,000
If I execute it see it is getting caught over here.

229
00:10:06,000 --> 00:10:13,000
So you can write any number of exceptions, but always make sure that the last exception class that

230
00:10:13,000 --> 00:10:17,000
we are using should be in the last section, right?

231
00:10:17,000 --> 00:10:22,000
We should not write this before zero division error or any other kind of exception, because this is

232
00:10:22,000 --> 00:10:27,000
the parent class from which all the exceptions are basically getting derived.

233
00:10:27,000 --> 00:10:27,000
Okay.

234
00:10:27,000 --> 00:10:31,000
And this is how we handle different kind of exceptions.

235
00:10:31,000 --> 00:10:32,000
Right.

236
00:10:32,000 --> 00:10:34,000
So I hope you have got this particular idea.

237
00:10:34,000 --> 00:10:38,000
But here what we have actually done is that we have handled multiple exceptions.

238
00:10:38,000 --> 00:10:39,000
Right.

239
00:10:39,000 --> 00:10:41,000
So let me just go ahead and show you one more example.

240
00:10:41,000 --> 00:10:43,000
Let's say I go ahead and try over here.

241
00:10:43,000 --> 00:10:45,000
I uh I'll create a number okay.

242
00:10:45,000 --> 00:10:47,000
And this will be my int.

243
00:10:47,000 --> 00:10:47,000
Okay.

244
00:10:47,000 --> 00:10:49,000
Let me just go ahead and write input.

245
00:10:49,000 --> 00:10:53,000
And here I will go ahead and write enter a number okay.

246
00:10:54,000 --> 00:10:54,000
Okay.

247
00:10:54,000 --> 00:10:59,000
Now once I enter a number over here, let's say that I go ahead and divide.

248
00:10:59,000 --> 00:11:05,000
This result is equal to ten divided by num okay ten divided by num.

249
00:11:05,000 --> 00:11:10,000
Now I will go ahead and catch or write this except block.

250
00:11:11,000 --> 00:11:11,000
Remember.

251
00:11:11,000 --> 00:11:17,000
Except block is basically to handle all the exception that arises inside the strike try block right.

252
00:11:17,000 --> 00:11:19,000
So I will just go ahead and write accept.

253
00:11:19,000 --> 00:11:19,000
Okay.

254
00:11:19,000 --> 00:11:24,000
Now inside this accept, uh, let's say that I will go ahead and use one value error.

255
00:11:24,000 --> 00:11:24,000
Okay.

256
00:11:25,000 --> 00:11:26,000
Uh, value error.

257
00:11:26,000 --> 00:11:28,000
And uh, here I will just go ahead and print.

258
00:11:28,000 --> 00:11:34,000
Let's say value error is specifically if I do not write a proper number over here.

259
00:11:34,000 --> 00:11:34,000
Right.

260
00:11:34,000 --> 00:11:37,000
So this is not a valid number.

261
00:11:37,000 --> 00:11:41,000
Not a valid number okay.

262
00:11:41,000 --> 00:11:44,000
Similarly I will go ahead and write accept another except block.

263
00:11:44,000 --> 00:11:48,000
And let's say here I'm just going to use zero division error okay.

264
00:11:48,000 --> 00:11:53,000
And this will basically say hey print print.

265
00:11:53,000 --> 00:12:01,000
You can't enter number enter denominator greater than zero okay.

266
00:12:01,000 --> 00:12:03,000
Now see how I'm handling right.

267
00:12:04,000 --> 00:12:07,000
This is the block of code where the exception will be handled.

268
00:12:07,000 --> 00:12:08,000
Uh exception will be raised.

269
00:12:08,000 --> 00:12:09,000
Right.

270
00:12:09,000 --> 00:12:15,000
Um, and this exception, like error will be coming over here and we will catch this particular exception

271
00:12:15,000 --> 00:12:17,000
either over here or either over here.

272
00:12:17,000 --> 00:12:18,000
Right.

273
00:12:18,000 --> 00:12:19,000
So let's see this.

274
00:12:19,000 --> 00:12:23,000
So if I go ahead and execute it, let's go ahead and say that I've given the number as zero.

275
00:12:23,000 --> 00:12:25,000
Now what will happen if I press enter.

276
00:12:26,000 --> 00:12:29,000
Just imagine obviously zero division error will get caught.

277
00:12:29,000 --> 00:12:33,000
So here you can see enter denominator greater than zero right.

278
00:12:33,000 --> 00:12:38,000
Let's say that if I go ahead and execute it and this time I give a string I give a value called a string

279
00:12:38,000 --> 00:12:38,000
crush.

280
00:12:38,000 --> 00:12:40,000
So now what will happen.

281
00:12:40,000 --> 00:12:41,000
The value error will get caught.

282
00:12:41,000 --> 00:12:43,000
And this is not a valid number right.

283
00:12:43,000 --> 00:12:46,000
So here clearly you can see that I'm able to handle it.

284
00:12:46,000 --> 00:12:48,000
Now there may be different different kind of errors.

285
00:12:48,000 --> 00:12:54,000
So what I will do finally I will just go ahead and write accept with exception as x.

286
00:12:54,000 --> 00:12:56,000
And I'll just go ahead and print X okay.

287
00:12:56,000 --> 00:12:59,000
Over here just to manage all the other errors.

288
00:12:59,000 --> 00:13:06,000
So let's say if I'm giving 34 this will get executed perfectly because I'm dividing the result by ten

289
00:13:06,000 --> 00:13:07,000
by that specific number.

290
00:13:07,000 --> 00:13:08,000
Okay.

291
00:13:08,000 --> 00:13:13,000
So here was an example to make you understand about try catch block.

292
00:13:13,000 --> 00:13:15,000
Or we also say try except block okay.

293
00:13:15,000 --> 00:13:19,000
Now I just want to also show you one more example.

294
00:13:19,000 --> 00:13:26,000
And here along with try except I'm also going to show you else block.

295
00:13:27,000 --> 00:13:27,000
Okay.

296
00:13:27,000 --> 00:13:29,000
Now let's go ahead and see this.

297
00:13:29,000 --> 00:13:30,000
So here is my try.

298
00:13:30,000 --> 00:13:33,000
Okay I will go ahead and create my number.

299
00:13:33,000 --> 00:13:34,000
And this will be input.

300
00:13:36,000 --> 00:13:38,000
And here let me just go ahead and write.

301
00:13:38,000 --> 00:13:39,000
Enter a number.

302
00:13:41,000 --> 00:13:41,000
Okay.

303
00:13:42,000 --> 00:13:43,000
Enter a number.

304
00:13:43,000 --> 00:13:48,000
Then I will just go ahead and say result is equal to ten divided by number.

305
00:13:48,000 --> 00:13:48,000
Okay.

306
00:13:49,000 --> 00:13:51,000
Let's say this is there okay.

307
00:13:51,000 --> 00:14:00,000
Now one exception that I'll try to handle is is value error and zero division error.

308
00:14:00,000 --> 00:14:00,000
Okay.

309
00:14:00,000 --> 00:14:02,000
So I've written it over here.

310
00:14:02,000 --> 00:14:07,000
Let's say if both this error are not coming that basically means this has got executed successfully,

311
00:14:07,000 --> 00:14:07,000
right?

312
00:14:08,000 --> 00:14:12,000
Uh, in the worst case, I will also go ahead and write my exception.

313
00:14:12,000 --> 00:14:13,000
Okay.

314
00:14:13,000 --> 00:14:18,000
So that basically means if this is also not getting caught over here, then that basically means the

315
00:14:18,000 --> 00:14:20,000
code has got successfully executed.

316
00:14:20,000 --> 00:14:20,000
Okay.

317
00:14:20,000 --> 00:14:21,000
Without any exception.

318
00:14:21,000 --> 00:14:22,000
Okay.

319
00:14:22,000 --> 00:14:27,000
But finally, let's say that I want to display this particular result value okay.

320
00:14:27,000 --> 00:14:30,000
Now the result value can be displayed uh, over here.

321
00:14:30,000 --> 00:14:32,000
And I will try to use one.

322
00:14:32,000 --> 00:14:36,000
amazing, uh, try catch functionality right over here, which is called as else.

323
00:14:37,000 --> 00:14:42,000
Now, let's say after this particular code has got executed, if exception is not happening, I want

324
00:14:42,000 --> 00:14:50,000
to close this entire try try except with with making sure that some lines of code needs to be executed

325
00:14:50,000 --> 00:14:50,000
anyhow.

326
00:14:51,000 --> 00:14:54,000
Okay, so there will be an option of writing else block.

327
00:14:54,000 --> 00:14:55,000
And this try is, uh.

328
00:14:55,000 --> 00:14:56,000
This else block is nothing.

329
00:14:56,000 --> 00:14:57,000
But it is.

330
00:14:57,000 --> 00:15:02,000
Uh, you can just say that, okay, at the end of the day, after this gets executed successfully,

331
00:15:02,000 --> 00:15:04,000
it has to execute this.

332
00:15:04,000 --> 00:15:04,000
Okay.

333
00:15:04,000 --> 00:15:10,000
So if I go ahead and write in the else so let's say in the else, I go ahead and say, hey the result

334
00:15:10,000 --> 00:15:15,000
is this specific result okay.

335
00:15:15,000 --> 00:15:17,000
So I'm just displaying the result over here.

336
00:15:17,000 --> 00:15:17,000
Right.

337
00:15:17,000 --> 00:15:20,000
So if I go ahead and execute it now see what will happen okay.

338
00:15:20,000 --> 00:15:21,000
Now what will happen?

339
00:15:21,000 --> 00:15:23,000
I will give the input let's say 23.

340
00:15:23,000 --> 00:15:25,000
If I press enter okay.

341
00:15:25,000 --> 00:15:27,000
You'll be able to see 23.

342
00:15:27,000 --> 00:15:28,000
No exception will be raised.

343
00:15:28,000 --> 00:15:30,000
Now when no exception is getting raised.

344
00:15:30,000 --> 00:15:32,000
So obviously this will get skipped.

345
00:15:32,000 --> 00:15:36,000
But at the end of the day this has to get executed right?

346
00:15:36,000 --> 00:15:39,000
So else is something like it has to probably get executed.

347
00:15:39,000 --> 00:15:44,000
Let's say if there is an exception over here, then one of the exception block will get executed and

348
00:15:44,000 --> 00:15:47,000
it will come outside of this try except block.

349
00:15:47,000 --> 00:15:51,000
But if this gets successfully executed anyhow, it is going to execute this else block.

350
00:15:51,000 --> 00:15:53,000
So if I go ahead and press enter.

351
00:15:53,000 --> 00:15:55,000
So here you can see the result is 0.434.

352
00:15:55,000 --> 00:15:58,000
Okay let's go ahead and execute this once again.

353
00:15:58,000 --> 00:16:00,000
And let me just go ahead and write zero.

354
00:16:00,000 --> 00:16:02,000
Now in case of zero what will happen.

355
00:16:02,000 --> 00:16:03,000
Zero division error is there.

356
00:16:03,000 --> 00:16:05,000
So you can't divide the number by zero.

357
00:16:05,000 --> 00:16:06,000
So I'll press enter.

358
00:16:06,000 --> 00:16:07,000
That is the error.

359
00:16:07,000 --> 00:16:09,000
But here you can see else block is not getting executed.

360
00:16:09,000 --> 00:16:14,000
So that basically means whenever an error happens or exception happens it will get handled in the except

361
00:16:14,000 --> 00:16:17,000
block at that point of time, else block will not get executed.

362
00:16:17,000 --> 00:16:22,000
But if none of the exception gets raised over here, then what will happen?

363
00:16:22,000 --> 00:16:23,000
Else has to execute anyhow.

364
00:16:23,000 --> 00:16:28,000
Okay, so this was an amazing example with respect to else, right?

365
00:16:28,000 --> 00:16:33,000
So where uh, you'll be able to see that Okay.

366
00:16:33,000 --> 00:16:37,000
If I am not getting an exception, then it will get it will execute.

367
00:16:37,000 --> 00:16:38,000
Okay.

368
00:16:38,000 --> 00:16:42,000
Otherwise, uh, if I'm getting an error, this is just going to stop.

369
00:16:42,000 --> 00:16:44,000
And it is not going to execute this else block.

370
00:16:44,000 --> 00:16:44,000
Okay?

371
00:16:44,000 --> 00:16:46,000
This is how you will be able to understand.

372
00:16:46,000 --> 00:16:48,000
Now this is fine.

373
00:16:49,000 --> 00:16:51,000
Let's try something more.

374
00:16:51,000 --> 00:16:51,000
Okay.

375
00:16:51,000 --> 00:16:59,000
Now this time what I'm actually going to do I'm going to use try except else I'm also going to use one

376
00:16:59,000 --> 00:17:03,000
more, one more amazing functionality or one more block, which is basically called as finally.

377
00:17:03,000 --> 00:17:06,000
Okay, now let's see one example with respect to this.

378
00:17:06,000 --> 00:17:08,000
Now what exactly is finally we'll discuss about this.

379
00:17:08,000 --> 00:17:12,000
Okay, I will just write a code and the same kind of code.

380
00:17:12,000 --> 00:17:14,000
I will just copy and paste it over here.

381
00:17:14,000 --> 00:17:15,000
So see this okay.

382
00:17:17,000 --> 00:17:19,000
So here is my number.

383
00:17:19,000 --> 00:17:19,000
Right.

384
00:17:19,000 --> 00:17:22,000
And here I'm probably dividing it.

385
00:17:22,000 --> 00:17:26,000
If exception does not happen over here, then directly else has to get executed.

386
00:17:26,000 --> 00:17:27,000
Okay?

387
00:17:27,000 --> 00:17:30,000
Else will get executed only when error is not there.

388
00:17:30,000 --> 00:17:31,000
Okay.

389
00:17:31,000 --> 00:17:32,000
Again I'm.

390
00:17:32,000 --> 00:17:33,000
Let me repeat it.

391
00:17:33,000 --> 00:17:35,000
This is going to execute.

392
00:17:35,000 --> 00:17:38,000
Get executed only when there is no error over here, right?

393
00:17:38,000 --> 00:17:41,000
If there is an error, it will be handled by this kind of exception.

394
00:17:41,000 --> 00:17:46,000
I'll also go ahead and write this exception over here, so that I will bring up multiple condition of

395
00:17:46,000 --> 00:17:47,000
exceptions.

396
00:17:47,000 --> 00:17:53,000
Okay, now finally, something finally is a kind of block weather error may be coming or not in this

397
00:17:53,000 --> 00:17:54,000
particular block of code.

398
00:17:54,000 --> 00:17:56,000
It has to execute anyhow.

399
00:17:57,000 --> 00:17:59,000
Okay, so let me repeat it again.

400
00:17:59,000 --> 00:18:03,000
We have already discussed just a while back about else.

401
00:18:03,000 --> 00:18:05,000
Right else is something like this.

402
00:18:05,000 --> 00:18:10,000
If no error is probably coming over here, then we will go ahead and execute this.

403
00:18:10,000 --> 00:18:15,000
If error is probably coming up, then else part will not get executed since it is getting handled in

404
00:18:15,000 --> 00:18:16,000
the exception block.

405
00:18:17,000 --> 00:18:25,000
In case of finally block whether the error may be coming here or not, it has to execute.

406
00:18:25,000 --> 00:18:27,000
Okay, now let me just show you.

407
00:18:27,000 --> 00:18:31,000
So let's say if I go ahead and write 12 in case of 12, obviously it is a good number.

408
00:18:31,000 --> 00:18:33,000
It is a proper input.

409
00:18:33,000 --> 00:18:35,000
So no error is getting handled.

410
00:18:35,000 --> 00:18:37,000
So else block will get executed.

411
00:18:37,000 --> 00:18:41,000
The print result something will come and after this finally block will also get executed.

412
00:18:41,000 --> 00:18:43,000
So if I go ahead and press enter.

413
00:18:43,000 --> 00:18:46,000
So here you can see the result is 0.833 and execution complete.

414
00:18:46,000 --> 00:18:46,000
Okay.

415
00:18:46,000 --> 00:18:48,000
Finally is also getting executed.

416
00:18:48,000 --> 00:18:56,000
Now in case of, in case of uh, let's say in the case of error, let's say if I go ahead and write

417
00:18:56,000 --> 00:18:57,000
over here zero.

418
00:18:57,000 --> 00:18:59,000
Now in case of zero, what will happen?

419
00:18:59,000 --> 00:19:00,000
Zero division error is coming up.

420
00:19:00,000 --> 00:19:01,000
Right.

421
00:19:01,000 --> 00:19:03,000
So x else block will not get executed.

422
00:19:03,000 --> 00:19:06,000
But finally block will definitely get executed.

423
00:19:06,000 --> 00:19:10,000
So if I go ahead and press enter you can see you can't divide by zero.

424
00:19:10,000 --> 00:19:12,000
That is zero division error.

425
00:19:12,000 --> 00:19:16,000
And finally basically gets executed which is called as execution complete.

426
00:19:16,000 --> 00:19:20,000
Now you may be thinking, Chris, where in the real world scenario this will come, right?

427
00:19:20,000 --> 00:19:22,000
Where do we specifically use final find block at all?

428
00:19:22,000 --> 00:19:25,000
Let me just give you one example guys.

429
00:19:25,000 --> 00:19:28,000
Let's say that in try block I am connecting to a database okay.

430
00:19:29,000 --> 00:19:34,000
Now in this particular database let's say I got some connection issues, user password issues and.

431
00:19:34,000 --> 00:19:38,000
All right um, or let's say that I have connected to the database.

432
00:19:38,000 --> 00:19:39,000
Then I did some kind of operation there.

433
00:19:39,000 --> 00:19:40,000
I got an exception.

434
00:19:41,000 --> 00:19:43,000
I handled that particular exception right.

435
00:19:43,000 --> 00:19:49,000
But at the end of the day, I need to close the connection of that particular database so I can write

436
00:19:49,000 --> 00:19:51,000
the code over here in the finally block.

437
00:19:51,000 --> 00:19:52,000
Right.

438
00:19:52,000 --> 00:19:55,000
Because at the end of the day, if I'm getting an exception.

439
00:19:55,000 --> 00:19:57,000
So that exception is basically getting handled.

440
00:19:57,000 --> 00:20:02,000
But if I come out of that particular block, I need to make sure that that entire connection gets closed.

441
00:20:02,000 --> 00:20:07,000
And that is how we can specifically use this try except else and finally block.

442
00:20:07,000 --> 00:20:07,000
Okay.

443
00:20:08,000 --> 00:20:11,000
Now this was some of the ways of handling exceptions.

444
00:20:11,000 --> 00:20:16,000
And again any kind of errors that you'll be seeing there will be different different exceptions which

445
00:20:16,000 --> 00:20:18,000
you can probably check it out.

446
00:20:18,000 --> 00:20:18,000
Okay.

447
00:20:19,000 --> 00:20:25,000
Uh, again uh, guys, uh, the thing is that unless and until you don't practice, you won't be able

448
00:20:25,000 --> 00:20:26,000
to become perfect.

449
00:20:26,000 --> 00:20:31,000
Please make sure that you have this all indentation techniques that is basically getting aligned.

450
00:20:31,000 --> 00:20:33,000
You can have multiple exception blocks.

451
00:20:33,000 --> 00:20:40,000
You can have multiple, uh, but you can also use this else and uh, finally block also to handle more

452
00:20:40,000 --> 00:20:41,000
diversified code errors.

453
00:20:42,000 --> 00:20:42,000
Okay.

454
00:20:43,000 --> 00:20:45,000
Now let's see some kind of practical examples.

455
00:20:45,000 --> 00:20:52,000
So first of all I will just go ahead and show you file handling and exception handling okay.

456
00:20:52,000 --> 00:20:54,000
This is most commonly used okay.

457
00:20:54,000 --> 00:21:00,000
So let me just go ahead and use a try block I will go ahead and open my file.

458
00:21:00,000 --> 00:21:04,000
So let's say file is equal to open open.

459
00:21:06,000 --> 00:21:10,000
Now you remember right in in previous videos when we were discussing about file operation, we tried

460
00:21:10,000 --> 00:21:15,000
to open a file which is called as example dot txt, but that was not present over there and it gave

461
00:21:15,000 --> 00:21:16,000
us an error.

462
00:21:16,000 --> 00:21:19,000
It gave us an exception saying that hey, this file does not exist.

463
00:21:19,000 --> 00:21:19,000
Okay.

464
00:21:19,000 --> 00:21:23,000
So we'll try to, uh, you know, do the same thing over here.

465
00:21:23,000 --> 00:21:28,000
So here I will just go ahead and open my example one dot txt file okay.

466
00:21:28,000 --> 00:21:30,000
And here I will open this in read mode.

467
00:21:30,000 --> 00:21:31,000
Let's see.

468
00:21:32,000 --> 00:21:36,000
And right now if I go ahead and show you in exception handling there is no file called as example one

469
00:21:36,000 --> 00:21:41,000
dot txt okay then uh I will just go ahead and write.

470
00:21:41,000 --> 00:21:48,000
Content is equal to file dot read will read the file inside that and we'll go ahead and print the content

471
00:21:48,000 --> 00:21:48,000
okay.

472
00:21:48,000 --> 00:21:52,000
So this is my try block where the exception may rise.

473
00:21:52,000 --> 00:21:57,000
Right now I will go ahead and uh handle this exception, so I will I know that this is a kind of file

474
00:21:57,000 --> 00:21:59,000
not found error okay.

475
00:21:59,000 --> 00:22:01,000
And because if the file does not exist.

476
00:22:01,000 --> 00:22:03,000
So this kind of exception I will be able to see.

477
00:22:03,000 --> 00:22:10,000
So here you can see the file does not exist okay.

478
00:22:10,000 --> 00:22:10,000
Okay.

479
00:22:11,000 --> 00:22:12,000
Okay.

480
00:22:12,000 --> 00:22:18,000
Now, anyhow, uh, in the finally block, let's go ahead and write this in the finally block.

481
00:22:18,000 --> 00:22:24,000
In the finally block, I'm going to make sure that at the end of the day, I need to close this particular

482
00:22:24,000 --> 00:22:24,000
file.

483
00:22:24,000 --> 00:22:24,000
Right.

484
00:22:24,000 --> 00:22:34,000
So I'll write if file if file in locals.

485
00:22:35,000 --> 00:22:37,000
Okay, now what exactly is locals?

486
00:22:41,000 --> 00:22:44,000
Returns a dictionary containing the current scope.

487
00:22:44,000 --> 00:22:45,000
Local variables.

488
00:22:45,000 --> 00:22:45,000
Okay.

489
00:22:45,000 --> 00:22:48,000
If it is not present in the local okay.

490
00:22:48,000 --> 00:22:51,000
And the file is not closed.

491
00:22:52,000 --> 00:22:54,000
Okay, I may get an exception over here also in the read also.

492
00:22:54,000 --> 00:22:55,000
Right.

493
00:22:55,000 --> 00:22:58,000
So that is the reason I'm writing this particular condition.

494
00:22:58,000 --> 00:23:05,000
If this two are if if that file is present in the local variable and it is not closed, I will go ahead

495
00:23:05,000 --> 00:23:08,000
and close this file because I cannot make this file open.

496
00:23:08,000 --> 00:23:12,000
You know, there may be some kind of issue similarly with respect to database connection.

497
00:23:12,000 --> 00:23:16,000
So I will go ahead and print file closed okay.

498
00:23:16,000 --> 00:23:22,000
At the end of the day, whatever may happen in the exception right in the finally block I am going to

499
00:23:22,000 --> 00:23:23,000
close this particular file.

500
00:23:23,000 --> 00:23:28,000
Now, if I go ahead and execute this, you'll be able to see the file does not exist is coming right.

501
00:23:28,000 --> 00:23:31,000
So here automatically the exception is basically getting handled.

502
00:23:31,000 --> 00:23:32,000
Right.

503
00:23:33,000 --> 00:23:35,000
Um, and let me do one thing.

504
00:23:35,000 --> 00:23:37,000
Let me just write a is equal to B over here.

505
00:23:37,000 --> 00:23:40,000
And let's say that okay fine I have some kind of file okay.

506
00:23:40,000 --> 00:23:41,000
I have some kind of file.

507
00:23:42,000 --> 00:23:47,000
Uh let's say here example one dot txt.

508
00:23:47,000 --> 00:23:49,000
Okay I have this file.

509
00:23:49,000 --> 00:23:50,000
Okay I have this file.

510
00:23:50,000 --> 00:23:51,000
It is an empty file.

511
00:23:51,000 --> 00:23:54,000
Now what will happen when I write e is equal to b?

512
00:23:54,000 --> 00:23:59,000
I know it is not a file found error, so I will just go ahead and handle it with the parent class exception

513
00:23:59,000 --> 00:24:00,000
as x.

514
00:24:00,000 --> 00:24:00,000
Okay.

515
00:24:01,000 --> 00:24:04,000
And I will just go ahead and print x okay.

516
00:24:04,000 --> 00:24:09,000
But at the end of the day I know the code will be giving me an error over here, but what I am doing

517
00:24:09,000 --> 00:24:11,000
because here I will be reading the file, right?

518
00:24:11,000 --> 00:24:13,000
So the file is in the open state yet.

519
00:24:13,000 --> 00:24:18,000
So when this error comes, I know this will not get handled by this, but instead it will get handled

520
00:24:18,000 --> 00:24:19,000
by this.

521
00:24:19,000 --> 00:24:21,000
In the finally block, I am making sure that I'll close the file.

522
00:24:21,000 --> 00:24:24,000
Okay, so that is how we will specifically do so.

523
00:24:24,000 --> 00:24:28,000
Here you can see that I am actually getting an error over here now instead of this.

524
00:24:28,000 --> 00:24:29,000
And I will just go ahead and use.

525
00:24:29,000 --> 00:24:30,000
Ah.

526
00:24:30,000 --> 00:24:34,000
So once I have used ah here you can see name B is not defined and file closed okay.

527
00:24:34,000 --> 00:24:38,000
So so we are at the end of the day we are just closing all the specific files okay.

528
00:24:39,000 --> 00:24:41,000
Uh some kind of errors will definitely be coming up.

529
00:24:41,000 --> 00:24:45,000
So we should always make sure that whenever we are opening the file, at the end of the day, we need

530
00:24:45,000 --> 00:24:46,000
to close this file.

531
00:24:46,000 --> 00:24:52,000
So I hope, uh, you are able to understand this, uh, entire thing and you are able to understand

532
00:24:52,000 --> 00:24:53,000
with multiple examples.

533
00:24:53,000 --> 00:24:55,000
I have shown you each and every thing step by step.

534
00:24:55,000 --> 00:24:56,000
We have executed each and every thing.

535
00:24:56,000 --> 00:24:58,000
So yes, this was it for my side.

536
00:24:58,000 --> 00:24:59,000
I will see you all in the next video.

537
00:24:59,000 --> 00:25:00,000
Thank you.

