1
00:00:05,460 --> 00:00:07,170
I mentioned in the last video

2
00:00:07,170 --> 00:00:08,790
there's just a couple more things we

3
00:00:08,790 --> 00:00:10,590
need to look at with exceptions

4
00:00:10,590 --> 00:00:12,420
I guess I said a couple of things but I

5
00:00:12,420 --> 00:00:15,320
really meant 4 so there's two optional

6
00:00:15,320 --> 00:00:17,280
clauses that we can add to a try

7
00:00:17,280 --> 00:00:20,190
statement else and finally so we'll have

8
00:00:20,190 --> 00:00:22,680
a look at what they are next

9
00:00:22,680 --> 00:00:25,110
now it can be important to consider the

10
00:00:25,110 --> 00:00:27,450
order that you catch exceptions using

11
00:00:27,450 --> 00:00:29,550
accept and i'm going to explain about

12
00:00:29,550 --> 00:00:31,950
that as well and the fourth thing is

13
00:00:31,950 --> 00:00:34,200
raising our own exceptions which you

14
00:00:34,200 --> 00:00:35,900
sometimes want to do in your functions

15
00:00:35,900 --> 00:00:38,420
if there's really no sensible action they

16
00:00:38,420 --> 00:00:40,760
can take your functions have to catch

17
00:00:40,760 --> 00:00:41,910
the exception

18
00:00:41,910 --> 00:00:43,740
otherwise your program will crash

19
00:00:43,740 --> 00:00:46,670
as we've seen but if you can't take any

20
00:00:46,670 --> 00:00:48,830
sensible action then sometimes the

21
00:00:48,830 --> 00:00:51,360
best approach is to pass the exception

22
00:00:51,360 --> 00:00:53,760
on to whatever called your function for

23
00:00:53,760 --> 00:00:55,860
it to deal with going to start with the

24
00:00:55,860 --> 00:00:58,290
finally Clause because our current

25
00:00:58,290 --> 00:01:00,170
example program provides quite a cool

26
00:01:00,170 --> 00:01:02,510
demonstration of the fact that a

27
00:01:02,510 --> 00:01:05,670
finally Claus will always be executed so

28
00:01:05,670 --> 00:01:07,860
a finally clause starts surprisingly

29
00:01:07,860 --> 00:01:10,140
with the keyword finally followed by the

30
00:01:10,140 --> 00:01:11,750
block of code that you want to execute

31
00:01:11,750 --> 00:01:14,720
whether an exception was raised or not

32
00:01:14,720 --> 00:01:17,820
let's go ahead and put the finaly clause

33
00:01:17,820 --> 00:01:20,430
into effect and do that on line 13 so...

34
00:01:20,430 --> 00:01:24,780
finally call on the next line as print

35
00:01:24,780 --> 00:01:28,160
...

36
00:01:28,160 --> 00:01:34,160
...now if you use

37
00:01:34,160 --> 00:01:37,070
a finaly Claus it must come after all

38
00:01:37,070 --> 00:01:39,780
your except causes right so let's just

39
00:01:39,780 --> 00:01:42,710
run this and i'm just going to add some

40
00:01:42,710 --> 00:01:46,910
valid input 10 and 5 notice the

41
00:01:46,910 --> 00:01:49,610
finally clause pop up there 4 and there's

42
00:01:49,610 --> 00:01:52,140
the finally executing again if we

43
00:01:52,140 --> 00:01:55,250
run this again and this time I do my

44
00:01:55,250 --> 00:01:57,320
contribute command trick and again

45
00:01:57,320 --> 00:02:01,070
ctrl D on windows or linux notice that

46
00:02:01,070 --> 00:02:03,050
the process still executed but the finally

47
00:02:03,050 --> 00:02:06,710
clause was still executed as well so the

48
00:02:06,710 --> 00:02:08,389
finally Claus really is executed

49
00:02:08,389 --> 00:02:10,669
regardless of whether an exception was

50
00:02:10,669 --> 00:02:13,250
handled or not that's particularly

51
00:02:13,250 --> 00:02:15,090
impressive considering what the code

52
00:02:15,090 --> 00:02:17,100
does when I terminate the input with

53
00:02:17,100 --> 00:02:18,400
control D

54
00:02:18,400 --> 00:02:20,440
so in the end of EOF or end of

55
00:02:20,440 --> 00:02:23,830
file error exception block on line 11 we

56
00:02:23,830 --> 00:02:25,960
we actually called sys.exit to

57
00:02:25,960 --> 00:02:27,850
terminate the program but even though

58
00:02:27,850 --> 00:02:30,100
the program's been told to terminate on

59
00:02:30,100 --> 00:02:32,620
line 12 the finally block on line

60
00:02:32,620 --> 00:02:36,190
fourteen is still executed first so if

61
00:02:36,190 --> 00:02:37,720
you think about it that really makes

62
00:02:37,720 --> 00:02:40,210
finally an ideal place for performing

63
00:02:40,210 --> 00:02:42,580
any tidying up that may be required

64
00:02:42,580 --> 00:02:45,250
before your code finishes now the sorts

65
00:02:45,250 --> 00:02:46,720
of things that you want to do in a

66
00:02:46,720 --> 00:02:48,310
finally block would be things like

67
00:02:48,310 --> 00:02:50,590
closing a database cursor or connection

68
00:02:50,590 --> 00:02:53,050
or closing any open files if your

69
00:02:53,050 --> 00:02:54,730
program is open any those types of

70
00:02:54,730 --> 00:02:56,770
things and that will actually use a

71
00:02:56,770 --> 00:02:58,360
finally clause when we get back to our

72
00:02:58,360 --> 00:03:00,430
database to make sure we close our

73
00:03:00,430 --> 00:03:01,210
cursors

74
00:03:01,210 --> 00:03:04,420
alright so moving on an else clause is

75
00:03:04,420 --> 00:03:06,370
executed if the code in the

76
00:03:06,370 --> 00:03:08,890
try block doesn't raise an exception

77
00:03:08,890 --> 00:03:10,750
so in that sense it's equivalent to the

78
00:03:10,750 --> 00:03:14,290
else clause in a four or a while loop if

79
00:03:14,290 --> 00:03:15,850
you remember the else

80
00:03:15,850 --> 00:03:18,310
in a while loop will execute if the loop

81
00:03:18,310 --> 00:03:20,950
terminates normally so it executed

82
00:03:20,950 --> 00:03:23,800
there's no break or return statement to

83
00:03:23,800 --> 00:03:27,220
execute the loop prematurely so the same

84
00:03:27,220 --> 00:03:29,170
is true of else when used with a try

85
00:03:29,170 --> 00:03:32,290
statement if no exception is handled the

86
00:03:32,290 --> 00:03:34,360
else clause will be executed

87
00:03:34,360 --> 00:03:37,090
otherwise it won't be now the use of the

88
00:03:37,090 --> 00:03:39,010
word else in these cases perhaps

89
00:03:39,010 --> 00:03:42,550
unfortunate it may help to pretend that

90
00:03:42,550 --> 00:03:44,410
a word like completion has been used

91
00:03:44,410 --> 00:03:47,380
instead but unfortunately it wasn't so

92
00:03:47,380 --> 00:03:48,970
we have to remember the two different

93
00:03:48,970 --> 00:03:50,170
uses of else

94
00:03:50,170 --> 00:03:52,480
an if statement and identifies a block

95
00:03:52,480 --> 00:03:54,940
that will be executed if no earlier

96
00:03:54,940 --> 00:03:57,580
condition is true and in for while and

97
00:03:57,580 --> 00:04:00,280
try it's executed if the previous

98
00:04:00,280 --> 00:04:02,740
code complete successfully so if you

99
00:04:02,740 --> 00:04:04,030
use the else clause in your try

100
00:04:04,030 --> 00:04:04,900
statements

101
00:04:04,900 --> 00:04:07,240
it must also come after all the except

102
00:04:07,240 --> 00:04:10,810
clauses but before any finally clause if

103
00:04:10,810 --> 00:04:11,560
you've got one

104
00:04:11,560 --> 00:04:14,170
alright so let's add an else clause to our

105
00:04:14,170 --> 00:04:15,880
program to see how it works

106
00:04:15,880 --> 00:04:17,290
we're going to print a message if the

107
00:04:17,290 --> 00:04:19,750
division was successful going to come

108
00:04:19,750 --> 00:04:23,440
down here after the code on line 23

109
00:04:23,440 --> 00:04:26,530
we're going to add an else on line 24

110
00:04:26,530 --> 00:04:29,910
...

111
00:04:29,910 --> 00:04:38,070
....okay

112
00:04:38,070 --> 00:04:41,520
so I run this again this time lets enter a

113
00:04:41,520 --> 00:04:47,820
couple of values so...we

114
00:04:47,820 --> 00:04:51,120
get 90 / 45 is 2.0 and division

115
00:04:51,120 --> 00:04:54,060
performed successfully so if we run it

116
00:04:54,060 --> 00:04:56,580
again

117
00:04:56,580 --> 00:04:58,500
90 this time but 0 for the second

118
00:04:58,500 --> 00:05:01,110
number you can't divide by 0

119
00:05:01,110 --> 00:05:03,090
and this time we haven't got the message

120
00:05:03,090 --> 00:05:05,370
division perform successfully on screen

121
00:05:05,370 --> 00:05:09,000
so how this is different to finally well

122
00:05:09,000 --> 00:05:11,310
the code in the else clause only execute

123
00:05:11,310 --> 00:05:14,130
if the triblock completed without raising

124
00:05:14,130 --> 00:05:15,390
an exception

125
00:05:15,390 --> 00:05:17,130
so when we attempted to divide by zero

126
00:05:17,130 --> 00:05:19,890
else code didn't execute if i change

127
00:05:19,890 --> 00:05:24,810
else on line 24 now to finally and run

128
00:05:24,810 --> 00:05:28,500
this again and repeat both tests so 90

129
00:05:28,500 --> 00:05:33,240
and 45 we correctly get the answer 2.0

130
00:05:33,240 --> 00:05:35,640
and division perform successfully run it

131
00:05:35,640 --> 00:05:38,550
again this time 90 and 0 as a

132
00:05:38,550 --> 00:05:40,980
second number and we get the message

133
00:05:40,980 --> 00:05:42,450
showing this time division performed

134
00:05:42,450 --> 00:05:45,300
successfully so finally is always executed

135
00:05:45,300 --> 00:05:47,880
even if there's an exception or as you saw

136
00:05:47,880 --> 00:05:49,320
earlier even if the program is

137
00:05:49,320 --> 00:05:52,620
terminated in the try block so code in

138
00:05:52,620 --> 00:05:54,690
the else clause only executed if the try

139
00:05:54,690 --> 00:05:57,120
completes it won't be executed if

140
00:05:57,120 --> 00:05:58,890
there's an exception or if the trial

141
00:05:58,890 --> 00:06:02,070
code returns or quits the program now

142
00:06:02,070 --> 00:06:03,510
you might be wondering this point why we

143
00:06:03,510 --> 00:06:06,030
didn't just put the division perform

144
00:06:06,030 --> 00:06:08,460
successfully message after the code on

145
00:06:08,460 --> 00:06:10,980
line 20 in other words after we printed

146
00:06:10,980 --> 00:06:13,530
out the result in this case we could

147
00:06:13,530 --> 00:06:14,700
have done just that

148
00:06:14,700 --> 00:06:16,500
that's because our print statement can't

149
00:06:16,500 --> 00:06:18,060
raise an exception so there's little

150
00:06:18,060 --> 00:06:20,550
point using else clause here else is

151
00:06:20,550 --> 00:06:22,710
really intended so that we don't

152
00:06:22,710 --> 00:06:24,990
accidentally catch an exception that

153
00:06:24,990 --> 00:06:27,060
wasn't raised by the code we're trying

154
00:06:27,060 --> 00:06:29,880
to protect with the try block so going

155
00:06:29,880 --> 00:06:32,070
back to our bank account example if you

156
00:06:32,070 --> 00:06:33,630
were protecting the updating of two

157
00:06:33,630 --> 00:06:36,600
transactions using a triblock would only

158
00:06:36,600 --> 00:06:38,780
want the code to roll back if there was

159
00:06:38,780 --> 00:06:40,970
an error saving the values to the two

160
00:06:40,970 --> 00:06:43,180
accounts if we add

161
00:06:43,180 --> 00:06:44,770
a load more code inside the try block

162
00:06:44,770 --> 00:06:47,470
and that code raises an exception then

163
00:06:47,470 --> 00:06:48,750
the transactions will be rolled back

164
00:06:48,750 --> 00:06:52,090
even if they completed successfully and

165
00:06:52,090 --> 00:06:53,710
will see that in a video coming up soon

166
00:06:53,710 --> 00:06:56,050
where it makes more sense to use an else

167
00:06:56,050 --> 00:06:58,690
clause so this last example is a bit

168
00:06:58,690 --> 00:07:00,690
contrived to demonstrate really how else

169
00:07:00,690 --> 00:07:02,560
and finally work and the difference

170
00:07:02,560 --> 00:07:03,490
between them

171
00:07:03,490 --> 00:07:04,720
now there's one last thing that this

172
00:07:04,720 --> 00:07:06,750
program is useful to demonstrate before

173
00:07:06,750 --> 00:07:08,590
we abandoned it and move on to something

174
00:07:08,590 --> 00:07:09,780
else

175
00:07:09,780 --> 00:07:11,470
now exceptions like everything else in

176
00:07:11,470 --> 00:07:14,820
python are objects in python three

177
00:07:14,820 --> 00:07:16,500
they must be derived from the base

178
00:07:16,500 --> 00:07:19,000
exception class or one of its subclasses

179
00:07:19,000 --> 00:07:21,970
that wasn't the case in python two

180
00:07:21,970 --> 00:07:24,400
although it was recommended and that's

181
00:07:24,400 --> 00:07:25,530
also mentioned at the start of the

182
00:07:25,530 --> 00:07:27,550
document we're looking at the browser

183
00:07:27,550 --> 00:07:31,090
earlier you can see here in Python

184
00:07:31,090 --> 00:07:32,560
all exceptions must be instance of a class

185
00:07:32,560 --> 00:07:35,530
instances of a class that derives from

186
00:07:35,530 --> 00:07:38,590
base exception so most exceptions will

187
00:07:38,590 --> 00:07:41,190
be subclasses of exception rather than

188
00:07:41,190 --> 00:07:43,210
base exception which is the

189
00:07:43,210 --> 00:07:44,740
recommendation in the fourth paragraph

190
00:07:44,740 --> 00:07:47,320
on that page programmers are encouraged to

191
00:07:47,320 --> 00:07:49,350
derive new exceptions from the exception

192
00:07:49,350 --> 00:07:51,280
class or one of its subclasses and not

193
00:07:51,280 --> 00:07:53,620
from base exception so why is that

194
00:07:53,620 --> 00:07:56,070
important well the first reason is that

195
00:07:56,070 --> 00:07:58,440
you can catch exception in your except

196
00:07:58,440 --> 00:08:00,030
Clause to catch any exception that's

197
00:08:00,030 --> 00:08:02,070
derived from it so let's actually have a

198
00:08:02,070 --> 00:08:03,430
look at how that works in our code

199
00:08:03,430 --> 00:08:08,740
so go back to the IDE so on line

200
00:08:08,740 --> 00:08:11,500
nine we've got this line except

201
00:08:11,500 --> 00:08:15,400
value error now we can

202
00:08:15,400 --> 00:08:17,130
check that value area is a subclass of

203
00:08:17,130 --> 00:08:19,120
exception by holding down the control

204
00:08:19,120 --> 00:08:21,490
key or on in my case on a Mac the

205
00:08:21,490 --> 00:08:23,500
command key clicking on it to see the

206
00:08:23,500 --> 00:08:26,820
code you can see their class value error

207
00:08:26,820 --> 00:08:28,590
exception then click on that and have a

208
00:08:28,590 --> 00:08:29,320
look

209
00:08:29,320 --> 00:08:31,780
so you can see here that value error

210
00:08:31,780 --> 00:08:34,630
inherits from exception and if we click

211
00:08:34,630 --> 00:08:38,340
on exception

212
00:08:38,340 --> 00:08:39,820
you can see that exception inherits from

213
00:08:39,820 --> 00:08:42,580
base exception so what that means is we

214
00:08:42,580 --> 00:08:43,710
can come back to our code gonna

215
00:08:43,710 --> 00:08:47,050
close it down means that we can actually

216
00:08:47,050 --> 00:08:49,710
change this on line 9 and replace that

217
00:08:49,710 --> 00:08:53,410
with exception and if we run this we

218
00:08:53,410 --> 00:08:55,250
should find that still copes

219
00:08:55,250 --> 00:08:59,360
with invalid input 90 and 0

220
00:08:59,360 --> 00:09:02,480
just bogus number valid number and then 0

221
00:09:02,480 --> 00:09:06,650
so still coping and basically

222
00:09:06,650 --> 00:09:08,660
catching those exceptions

223
00:09:08,660 --> 00:09:10,850
now with that said I'm not recommending that

224
00:09:10,850 --> 00:09:12,500
you do this or suggesting you do that

225
00:09:12,500 --> 00:09:14,180
it's generally a good idea to be

226
00:09:14,180 --> 00:09:16,070
specific in the exceptions you catch

227
00:09:16,070 --> 00:09:19,100
rather than just catching everything one

228
00:09:19,100 --> 00:09:20,710
reason for being specific is that you

229
00:09:20,710 --> 00:09:22,250
can handle the different exceptions

230
00:09:22,250 --> 00:09:24,200
differently now if you can just catch

231
00:09:24,200 --> 00:09:25,700
all exceptions as we're doing here

232
00:09:25,700 --> 00:09:27,860
the code will react in the same way

233
00:09:27,860 --> 00:09:30,560
regardless of what exception was raised

234
00:09:30,560 --> 00:09:33,920
that may often not be the appropriate

235
00:09:33,920 --> 00:09:36,230
way to react and in fact we've got a

236
00:09:36,230 --> 00:09:38,360
problem with our code now so it looks

237
00:09:38,360 --> 00:09:40,490
like it worked fine until you understand

238
00:09:40,490 --> 00:09:41,770
this next bit you probably won't see

239
00:09:41,770 --> 00:09:44,750
anything wrong by looking at the code now

240
00:09:44,750 --> 00:09:46,820
IntelliJ is aware that

241
00:09:46,820 --> 00:09:48,710
something is not quite right

242
00:09:48,710 --> 00:09:50,660
we actually got a warning on line 11

243
00:09:50,660 --> 00:09:52,820
that's where we catch the end of file

244
00:09:52,820 --> 00:09:55,490
error you can see the exception

245
00:09:55,490 --> 00:09:58,010
superclass of exception class EOF error

246
00:09:58,010 --> 00:10:00,170
has already been caught so let's

247
00:10:00,170 --> 00:10:01,610
actually see the effect of that before

248
00:10:01,610 --> 00:10:03,050
talking anymore about it i'm going to

249
00:10:03,050 --> 00:10:05,720
run it again going to do the ctrl D

250
00:10:05,720 --> 00:10:10,700
command D on the mac you can see what's

251
00:10:10,700 --> 00:10:12,140
happening here we're going into a loop

252
00:10:12,140 --> 00:10:14,510
our programs running wild again i'm

253
00:10:14,510 --> 00:10:17,660
going to stop it and cancel out of that

254
00:10:17,660 --> 00:10:20,420
the question at this point is what went

255
00:10:20,420 --> 00:10:21,110
wrong

256
00:10:21,110 --> 00:10:23,510
remember when I said that the order that

257
00:10:23,510 --> 00:10:25,760
you handle the exceptions is important

258
00:10:25,760 --> 00:10:28,340
what's happening here with our code is

259
00:10:28,340 --> 00:10:31,370
that exception is a subclass of EOF

260
00:10:31,370 --> 00:10:34,400
error now subclass of a base class is

261
00:10:34,400 --> 00:10:36,830
also an instance of its base class so

262
00:10:36,830 --> 00:10:39,110
this case EOF error is an

263
00:10:39,110 --> 00:10:41,210
instance of exception and when we catch

264
00:10:41,210 --> 00:10:44,270
exception on line 9 we're

265
00:10:44,270 --> 00:10:45,740
actually catching all exceptions that

266
00:10:45,740 --> 00:10:48,680
inherit from it including the eof error

267
00:10:48,680 --> 00:10:50,750
exception that has been defined on line

268
00:10:50,750 --> 00:10:53,060
11 so what that means is that the code

269
00:10:53,060 --> 00:10:55,880
that runs when eof error is raised is the

270
00:10:55,880 --> 00:10:58,880
actual code on line 10 so therefore the

271
00:10:58,880 --> 00:11:00,920
program doesn't get to exit it just

272
00:11:00,920 --> 00:11:03,410
prints a message and loops or allows the

273
00:11:03,410 --> 00:11:05,270
loop to go around again and again and

274
00:11:05,270 --> 00:11:07,670
again because the input streams closed

275
00:11:07,670 --> 00:11:08,870
and we keep getting these

276
00:11:08,870 --> 00:11:12,470
eof errors raised by line 7 IntelliJ is

277
00:11:12,470 --> 00:11:13,760
aware of this and that's why its giving

278
00:11:13,760 --> 00:11:14,690
us a warning

279
00:11:14,690 --> 00:11:16,940
so when you're handling multiple

280
00:11:16,940 --> 00:11:19,550
exceptions like we are here it is very

281
00:11:19,550 --> 00:11:20,900
important that you catch them in the

282
00:11:20,900 --> 00:11:21,800
right order

283
00:11:21,800 --> 00:11:24,320
so the more specific exceptions have to

284
00:11:24,320 --> 00:11:26,870
appear first so you can fix this program

285
00:11:26,870 --> 00:11:30,680
up by moving lines 11 and 12 up until

286
00:11:30,680 --> 00:11:32,180
before we catch the exception on line 9

287
00:11:32,180 --> 00:11:34,670
so I'm gonna just copy those cut

288
00:11:34,670 --> 00:11:38,030
them out and pasted above the code that

289
00:11:38,030 --> 00:11:40,880
was on line 9 and if you actually run

290
00:11:40,880 --> 00:11:44,870
this and enter some bogus text that isn't a

291
00:11:44,870 --> 00:11:48,020
number here we get invalid number and

292
00:11:48,020 --> 00:11:50,120
now if I come back here again and do a

293
00:11:50,120 --> 00:11:52,310
command on a Mac ctrl D on windows or

294
00:11:52,310 --> 00:11:55,190
linux we actually get it to exit

295
00:11:55,190 --> 00:11:57,440
correctly with exit code one so you can see

296
00:11:57,440 --> 00:11:59,630
the code that we generated for the

297
00:11:59,630 --> 00:12:02,420
exception eof error is now being

298
00:12:02,420 --> 00:12:04,670
executed correctly when we do the

299
00:12:04,670 --> 00:12:07,670
control d e so it's not often

300
00:12:07,670 --> 00:12:09,920
that you specify exception as the

301
00:12:09,920 --> 00:12:11,930
exception to handle your except Clause

302
00:12:11,930 --> 00:12:13,820
but this applies whether you're catching

303
00:12:13,820 --> 00:12:15,860
multiple exceptions and some of the ones

304
00:12:15,860 --> 00:12:18,560
you want to catch derive from others and

305
00:12:18,560 --> 00:12:20,180
by the way we are getting this other error here

306
00:12:20,180 --> 00:12:23,600
about two broad exception clause this

307
00:12:23,600 --> 00:12:24,680
warning is that perhaps we should be

308
00:12:24,680 --> 00:12:25,700
more specific

309
00:12:25,700 --> 00:12:27,110
alright so the last thing i want to

310
00:12:27,110 --> 00:12:29,870
mention is related to this now remember

311
00:12:29,870 --> 00:12:31,970
from the documentation that not all

312
00:12:31,970 --> 00:12:34,220
exceptions have to be derived from

313
00:12:34,220 --> 00:12:36,590
exception the requirement is that they

314
00:12:36,590 --> 00:12:38,990
derived from base exception or one of

315
00:12:38,990 --> 00:12:41,510
its subclasses so if you really do want

316
00:12:41,510 --> 00:12:44,180
to catch any exception then there's a

317
00:12:44,180 --> 00:12:46,070
way to specify the sort of wild card

318
00:12:46,070 --> 00:12:49,190
exception meaning everything to do that

319
00:12:49,190 --> 00:12:51,680
just leave off the exception type so

320
00:12:51,680 --> 00:12:53,600
the except Clause is just the keyword

321
00:12:53,600 --> 00:12:56,840
accept and a colon so this example is

322
00:12:56,840 --> 00:12:59,360
going to delete the exception on line 11

323
00:12:59,360 --> 00:13:02,840
and just leave the colon there before I

324
00:13:02,840 --> 00:13:05,030
do this just to be clear I'm going to

325
00:13:05,030 --> 00:13:07,340
change the finally here on line 24 to an else

326
00:13:07,340 --> 00:13:07,850
again

327
00:13:07,850 --> 00:13:10,880
so that when I make the code

328
00:13:10,880 --> 00:13:12,560
available in the resources it's showing

329
00:13:12,560 --> 00:13:14,030
how it should be done rather

330
00:13:14,030 --> 00:13:16,550
demonstrating how not to do it right the

331
00:13:16,550 --> 00:13:18,230
other thing I'm going to do here is on

332
00:13:18,230 --> 00:13:20,750
line 11 is just to make it clear here

333
00:13:20,750 --> 00:13:21,870
with a comment

334
00:13:21,870 --> 00:13:27,060
we should really be except...

335
00:13:27,060 --> 00:13:31,680
...

336
00:13:31,680 --> 00:13:33,180
alright let's test the program a few

337
00:13:33,180 --> 00:13:36,710
times let's try a number or partial

338
00:13:36,710 --> 00:13:38,460
numbers i will test for invalid input

339
00:13:38,460 --> 00:13:42,930
using numbers and letters invalid number

340
00:13:42,930 --> 00:13:47,070
will try just letters that works and try

341
00:13:47,070 --> 00:13:55,740
76.....that works ok running

342
00:13:55,740 --> 00:13:59,730
again and again we're getting the errors

343
00:13:59,730 --> 00:14:02,040
caught successfully invalid numbers by

344
00:14:02,040 --> 00:14:04,350
the Wildcard accept on line 11 so that's

345
00:14:04,350 --> 00:14:06,770
good right second time lets do a divided

346
00:14:06,770 --> 00:14:11,190
by 0 so 32 and 0 the finally clause always

347
00:14:11,190 --> 00:14:13,580
execute and you can't divide by 0 and

348
00:14:13,580 --> 00:14:15,540
we done a assist.exit 2 as you can see

349
00:14:15,540 --> 00:14:18,420
there the code on line 23 was executed and

350
00:14:18,420 --> 00:14:21,180
let's do the control d check again run

351
00:14:21,180 --> 00:14:24,510
that and do a control command D on a mac

352
00:14:24,510 --> 00:14:26,250
and control d on a windows

353
00:14:26,250 --> 00:14:29,070
or linux and the finally clause

354
00:14:29,070 --> 00:14:31,080
always execute and we're finishing the

355
00:14:31,080 --> 00:14:34,230
code on line 10 is executed

356
00:14:34,230 --> 00:14:35,010
correctly

357
00:14:35,010 --> 00:14:37,170
so everything's working so

358
00:14:37,170 --> 00:14:38,960
that's exceptions and how to handle them

359
00:14:38,960 --> 00:14:41,340
in python so in the next video we can

360
00:14:41,340 --> 00:14:42,630
have a quick look at how to raise our

361
00:14:42,630 --> 00:14:45,570
own exceptions before we ultimately get

362
00:14:45,570 --> 00:14:47,580
back to databases so see you as

363
00:14:47,580 --> 00:14:53,690
always in the next video

