1
00:00:05,240 --> 00:00:09,590
Welcome back, everyone to this lecture or we're going to be discussing errors and exception handling.

2
00:00:11,050 --> 00:00:14,350
When people are using our website later on, we learn about jingo.

3
00:00:14,710 --> 00:00:20,170
There are going to be instances where we don't have complete information to fulfill their web page request,

4
00:00:20,470 --> 00:00:25,030
and we need to make sure the site functions can still operate even if an error were to occur.

5
00:00:25,510 --> 00:00:29,110
When you're dealing with something like a website that has user interactions.

6
00:00:29,380 --> 00:00:32,920
You can't know every single possible user interaction ahead of time.

7
00:00:33,160 --> 00:00:38,710
So you have to assume that sometimes the user may make a mistake or we just don't have a web page for

8
00:00:38,710 --> 00:00:40,030
what the user is looking for.

9
00:00:41,110 --> 00:00:46,780
For example, you've seen Web pages that return a 404 error page if the page doesn't exist, or maybe

10
00:00:46,780 --> 00:00:49,090
the user entered the wrong domain URL extension.

11
00:00:49,390 --> 00:00:51,850
We don't want our entire website just to break down.

12
00:00:52,060 --> 00:00:55,990
So we need a way of actually taking into account that an error may occur.

13
00:00:56,380 --> 00:01:01,060
We'll need a way to try to execute a block of code, but continue to another block if the code doesn't

14
00:01:01,060 --> 00:01:01,420
work.

15
00:01:02,970 --> 00:01:07,800
Now, you've definitely already seen Python issue an exception error when you've made a small programming

16
00:01:07,800 --> 00:01:09,420
mistake when learning Python.

17
00:01:09,720 --> 00:01:13,440
And this is where Python tries to inform you of the specific error taking place.

18
00:01:13,770 --> 00:01:18,990
For example, i o error, which is an input output error, or maybe even a type error, which is the

19
00:01:18,990 --> 00:01:21,270
wrong data types are trying to work together.

20
00:01:21,660 --> 00:01:26,340
And you can actually write code for specific error situations so you can actually try to preemptively

21
00:01:26,340 --> 00:01:32,130
understand or relay a message for specific errors that you think may come out ahead of time.

22
00:01:33,540 --> 00:01:36,390
Now, in order to do this with Python, we actually use three keywords.

23
00:01:36,720 --> 00:01:41,910
It is the tri keyword and this is the block of code to be attempted, which may or may not lead to an

24
00:01:41,910 --> 00:01:42,210
error.

25
00:01:42,600 --> 00:01:45,480
There is the accept keyword, which is the block of code.

26
00:01:45,660 --> 00:01:48,810
We're going to execute in case there is an error in the try block.

27
00:01:49,080 --> 00:01:54,570
And then there's also an optional finally block of code, and that's a final block of code to be executed

28
00:01:54,570 --> 00:01:56,070
regardless of an error.

29
00:01:57,910 --> 00:02:04,390
Let's explore understanding exception codes and how to use try, except finally blocks to navigate potentially

30
00:02:04,390 --> 00:02:05,770
unknown errors in the future.

31
00:02:06,050 --> 00:02:07,660
I'm going to head over to Visual Studio now.

32
00:02:07,990 --> 00:02:09,820
All right, here I am in Visual Studio.

33
00:02:10,210 --> 00:02:16,090
Now there is a specific type of error that we can't really actually try to catch and then continue to

34
00:02:16,090 --> 00:02:16,660
do something.

35
00:02:16,930 --> 00:02:19,300
And that specifically is a syntax error.

36
00:02:19,540 --> 00:02:24,070
So I want to quickly point that out right now that you can't really catch this error because this particular

37
00:02:24,070 --> 00:02:28,150
type of error doesn't even allow the Python code to actually compile.

38
00:02:28,390 --> 00:02:30,850
And that's the kind of error where you're saying something like print.

39
00:02:31,390 --> 00:02:37,000
And then maybe for some reason you forgot to actually close off that particular block of code.

40
00:02:37,000 --> 00:02:38,260
So you say something like, hello.

41
00:02:38,560 --> 00:02:42,650
But note that I never actually have that closing quote.

42
00:02:42,670 --> 00:02:45,470
And in fact, Visual Studio code is actually trying to warn me of this.

43
00:02:45,470 --> 00:02:47,620
So it says, Hey, there's a problem here.

44
00:02:47,650 --> 00:02:51,190
No quick fix unless you actually close off that quote.

45
00:02:51,400 --> 00:02:55,930
But I'm going to save this, and I want to point out this sort of error where you just your code is

46
00:02:55,930 --> 00:02:58,000
actually not even be able to compile.

47
00:02:58,240 --> 00:02:59,860
That's something we can't actually catch.

48
00:02:59,880 --> 00:03:02,770
We have to fix that in our code before we actually run anything.

49
00:03:02,950 --> 00:03:06,310
But I do want to run this so you can see what an exception looks like.

50
00:03:06,910 --> 00:03:11,310
So I'm going to type out Python example that pi hit this and know this.

51
00:03:11,320 --> 00:03:13,990
This is specifically a syntax error.

52
00:03:14,200 --> 00:03:16,990
And it says end of line while scanning string literal.

53
00:03:17,020 --> 00:03:20,770
So you never actually figured out, Hey, where is the end of the quote?

54
00:03:20,770 --> 00:03:25,900
Their syntax errors are not something we can particularly try to grab in advance because it doesn't

55
00:03:25,900 --> 00:03:27,400
let the code run to begin with.

56
00:03:27,790 --> 00:03:30,400
So what kind of errors can we actually run in advance?

57
00:03:30,610 --> 00:03:36,760
Well, let's imagine I'm trying to print out the results of 10 plus 10, but maybe somewhere along the

58
00:03:36,760 --> 00:03:43,000
line I accidentally and trying to do the string 10 plus the integer 10.

59
00:03:44,200 --> 00:03:49,210
Now this may make sense if maybe we actually grabbed user input, but for whatever reason, we forgot

60
00:03:49,210 --> 00:03:52,030
to convert that input to an integer and it's actually a string.

61
00:03:52,240 --> 00:03:57,130
Maybe were grabbing this information from a database that stores numbers as strings and we're trying

62
00:03:57,130 --> 00:03:58,180
to add something like this.

63
00:03:58,480 --> 00:04:02,140
Clearly, we can't actually do this because there are two different data types.

64
00:04:02,500 --> 00:04:07,630
So if I try to run this code, it says, Hey, now, instead of a syntax error, you have a type error

65
00:04:08,020 --> 00:04:12,400
and it can only concatenate a string to another string, not an integer.

66
00:04:12,910 --> 00:04:14,350
So we have a mismatch here.

67
00:04:14,890 --> 00:04:20,050
Now this is the type of error, since it's not a syntax error that I can try to catch and do something

68
00:04:20,050 --> 00:04:20,680
beforehand.

69
00:04:21,010 --> 00:04:22,630
And the way this works is.

70
00:04:23,560 --> 00:04:25,970
I use what's known as a tri block.

71
00:04:26,050 --> 00:04:29,680
So I say, try colon and then here.

72
00:04:29,710 --> 00:04:33,400
You notice it's kind of like a function where we start having an indented block of code.

73
00:04:33,790 --> 00:04:36,500
Here's the block of code that I want to attempt to try.

74
00:04:36,850 --> 00:04:41,560
Keep in mind, this block of code can work perfectly fine, so I can say print.

75
00:04:42,730 --> 00:04:44,320
Something like hello here.

76
00:04:44,890 --> 00:04:47,050
So I'm going to try to print hello.

77
00:04:47,560 --> 00:04:50,770
Now let's imagine there's a mistake and this actually doesn't work.

78
00:04:50,890 --> 00:04:52,180
What's the next block of code?

79
00:04:52,960 --> 00:04:59,440
That is where the key word except comes into play and with just accept that's going to allow you to

80
00:04:59,440 --> 00:05:03,730
actually do something if there is an exception or error raised.

81
00:05:04,180 --> 00:05:05,200
So I'm going to print out.

82
00:05:06,710 --> 00:05:07,100
Error.

83
00:05:09,110 --> 00:05:13,790
So I'm going to save that so simple as blocks of codes or just to try something, and if there's an

84
00:05:13,790 --> 00:05:14,330
exception.

85
00:05:14,570 --> 00:05:16,520
Go ahead and do something here.

86
00:05:16,520 --> 00:05:20,690
I'm just using print statements that are just really simple functions that are being called.

87
00:05:20,870 --> 00:05:23,660
But obviously this could be way more complex blocks of code.

88
00:05:23,960 --> 00:05:29,630
And often when we're actually dealing with this and jingo, I'm going to have the try block have something

89
00:05:29,630 --> 00:05:31,310
to do with a user interaction.

90
00:05:31,310 --> 00:05:34,710
I may be calling a function there that asks for input from a user.

91
00:05:35,000 --> 00:05:40,340
Typically, if the mistake is going to happen completely internally for an old code, we're just going

92
00:05:40,340 --> 00:05:42,710
to actually fix it before we put it into a try block.

93
00:05:42,740 --> 00:05:47,570
So think of these try blocks as something that has some sort of user interactivity.

94
00:05:48,140 --> 00:05:49,910
So I will go ahead and save that here.

95
00:05:50,480 --> 00:05:51,960
And let's run this for right now.

96
00:05:51,980 --> 00:05:54,380
Notice I will be able to print hello.

97
00:05:55,220 --> 00:05:56,740
So I just get the output.

98
00:05:56,750 --> 00:05:57,200
Hello.

99
00:05:57,230 --> 00:06:02,840
The except block does not execute because we were successful in trying to print this out.

100
00:06:03,350 --> 00:06:06,800
Now let's see what happens if I have something which is a mistake again.

101
00:06:07,190 --> 00:06:12,740
For example, the string 10 plus the integer 10, I saved that change.

102
00:06:13,490 --> 00:06:16,850
And now when I run this, I get back error.

103
00:06:17,360 --> 00:06:18,650
So this is kind of interesting.

104
00:06:19,160 --> 00:06:25,880
I now am able to actually report something very specific, even if I had something that previously would

105
00:06:25,880 --> 00:06:26,960
have a type error.

106
00:06:27,170 --> 00:06:32,990
Recall that previously when we ran this, python just said, Hey, if I see right here you have a type

107
00:06:32,990 --> 00:06:37,730
error, you can only concatenate it string with a string, and it just pretty much ended the Python

108
00:06:37,730 --> 00:06:38,090
code.

109
00:06:38,570 --> 00:06:44,630
Now, with try and accept working in conjunction, I can actually report a custom message.

110
00:06:45,080 --> 00:06:51,260
Now here's what's even more interesting I can try to accept and catch specific errors, and we can already

111
00:06:51,260 --> 00:06:53,090
see that there's many different types of errors.

112
00:06:53,390 --> 00:06:56,330
You can check out the documentation for all the different exception errors.

113
00:06:56,330 --> 00:07:00,560
There are the for example, this specific one is called a type error.

114
00:07:00,950 --> 00:07:02,090
So let's actually try to catch that.

115
00:07:02,480 --> 00:07:03,170
How do we do this?

116
00:07:03,380 --> 00:07:10,760
Well, what I could say is in front of, except I can accept a particular type of exception error code.

117
00:07:11,210 --> 00:07:15,380
And again, you can check out the full list of built in exceptions in the documentation.

118
00:07:15,590 --> 00:07:18,920
It's actually just a Google search of Python Docs exceptions.

119
00:07:19,160 --> 00:07:21,620
You can check out the link in our actual file for this.

120
00:07:21,950 --> 00:07:26,330
But what you do is you just put in the error type that you're expecting to catch.

121
00:07:26,750 --> 00:07:33,620
So you can say accept type error and then because you know that this block of code only executes on

122
00:07:33,620 --> 00:07:40,220
that specific error, I can have a more custom message reported back so I can say something like you

123
00:07:41,090 --> 00:07:46,040
are using the wrong data types save that change.

124
00:07:46,040 --> 00:07:50,630
And now when I run this piece of code, let me expand this a little bit here.

125
00:07:51,550 --> 00:07:55,450
So when I run this piece of code after saving it, I get back the message, you're using the wrong data

126
00:07:55,450 --> 00:07:55,870
types.

127
00:07:56,350 --> 00:08:00,400
But what I can do is, let's say there's multiple errors that could occur.

128
00:08:00,430 --> 00:08:02,020
Maybe there's an input output error.

129
00:08:02,500 --> 00:08:07,300
Well, you can actually stack accept statements looking for different errors.

130
00:08:07,300 --> 00:08:11,170
So there's also what's known as an i o error, which is input output.

131
00:08:11,560 --> 00:08:17,530
So I could say print, you have an input output error.

132
00:08:17,920 --> 00:08:22,180
And what's nice about this is later on, OK, actually, if I really know what the code is attempting

133
00:08:22,180 --> 00:08:26,740
to do, I could actually pass on suggestions here so I could say something.

134
00:08:27,640 --> 00:08:33,220
Did you check the file permissions or something like that so you can actually be really useful?

135
00:08:33,250 --> 00:08:35,710
This is nice for when we're working off our own websites.

136
00:08:35,890 --> 00:08:40,090
We could report back to the user, Hey, are you sure you passed in the correct domain because I can't

137
00:08:40,090 --> 00:08:42,460
find that specific URL you're asking for?

138
00:08:42,700 --> 00:08:48,250
If I'm looking for a specific error, so when you run this notice, I'm not checking for two exceptions.

139
00:08:48,970 --> 00:08:49,690
I run that.

140
00:08:49,870 --> 00:08:55,570
And what happens is, if it's not this exception, it continues to go down to the type error exception

141
00:08:55,900 --> 00:08:57,280
and you can keep doing this.

142
00:08:57,280 --> 00:09:03,010
But keep in mind, if you don't know the particular type of exception or error that's going to occur,

143
00:09:03,460 --> 00:09:09,430
you can just type out, except with no particular exception code and then print whatever you want.

144
00:09:09,490 --> 00:09:11,080
Hey, you got an error?

145
00:09:11,620 --> 00:09:13,240
Keep in mind, it doesn't have to be a print function.

146
00:09:13,240 --> 00:09:15,880
You can really execute any Python block of code there.

147
00:09:17,550 --> 00:09:21,900
And it says, hey, you're using the wrong data types again, but this still works, I'm going to delete

148
00:09:22,230 --> 00:09:22,950
type error.

149
00:09:24,910 --> 00:09:25,240
Save.

150
00:09:26,200 --> 00:09:31,990
Run this again, and it says, Hey, you got an error, so notice here I can check for particular errors

151
00:09:31,990 --> 00:09:35,650
and then if there's an exception, I can print, Hey, you got an error.

152
00:09:36,310 --> 00:09:43,240
What I can also do is add in an else, so I could say, try this, look for an exception and else do

153
00:09:43,240 --> 00:09:43,950
something else.

154
00:09:44,020 --> 00:09:46,910
So if there is no exception, then this block.

155
00:09:46,930 --> 00:09:48,400
So, for example, I can say else.

156
00:09:50,960 --> 00:09:51,530
Prince.

157
00:09:53,380 --> 00:09:54,520
Elle Spurlock ran.

158
00:09:56,230 --> 00:09:57,880
So I saved this.

159
00:09:59,900 --> 00:10:05,450
Run this, and it says, Hey, you got an error, but let's actually say try 10 plus 10.

160
00:10:05,750 --> 00:10:07,010
So this is actually OK.

161
00:10:07,040 --> 00:10:08,120
There won't be an error here.

162
00:10:09,380 --> 00:10:13,910
I run this and I get 20 and it says L Block ran.

163
00:10:14,690 --> 00:10:19,670
Now, let's imagine you actually want this code to execute, even if there is an error.

164
00:10:20,270 --> 00:10:23,600
So right now, I showed you a try except else.

165
00:10:24,020 --> 00:10:29,180
However, else blocks are only going to run here if you don't end up having an exception.

166
00:10:29,750 --> 00:10:34,400
What if you do have an exception, but also want this block of code to execute?

167
00:10:34,790 --> 00:10:38,960
That's where the final block of code comes into play.

168
00:10:39,050 --> 00:10:44,000
And this is typically the most common way of actually organizing errors and exception handling.

169
00:10:44,270 --> 00:10:46,040
It's the block of code you want to try.

170
00:10:46,640 --> 00:10:49,310
The exception error message you want to report back.

171
00:10:49,790 --> 00:10:52,010
And then finally, you want to do something else.

172
00:10:52,490 --> 00:10:53,630
Note that finally.

173
00:10:54,950 --> 00:10:58,880
Will always run air or no air.

174
00:10:59,780 --> 00:11:04,250
So it's like finally, after you attempt at all this, go ahead and do something like that.

175
00:11:04,730 --> 00:11:08,420
So I'm going to save this note that right now I don't have a specific air.

176
00:11:08,780 --> 00:11:12,290
In fact, I'm going to just simplify this and delete that input output error.

177
00:11:12,710 --> 00:11:14,780
So I'm saying try to print 10 plus 10.

178
00:11:15,290 --> 00:11:20,150
If there's an error print, hey, you got an error and then finally go ahead and execute this block

179
00:11:20,150 --> 00:11:20,510
of code.

180
00:11:21,230 --> 00:11:24,440
So I run this and I get back 20 because it worked.

181
00:11:24,530 --> 00:11:27,050
And it says, finally, we'll always run error or no error.

182
00:11:27,410 --> 00:11:28,970
Now let's institute an error.

183
00:11:29,910 --> 00:11:37,170
And that would change this to be a string 10 plus 10, save those changes and run this and it says,

184
00:11:37,170 --> 00:11:38,430
Hey, you got an error.

185
00:11:38,640 --> 00:11:41,400
And the final block runs again.

186
00:11:41,970 --> 00:11:43,920
And keep in mind if this was an else.

187
00:11:45,380 --> 00:11:49,010
Like this, I wouldn't have seen that I would have run this.

188
00:11:51,050 --> 00:11:56,570
And it just says, hey, you got an error if you always want this to run air or no air, then you put

189
00:11:56,570 --> 00:11:56,810
in.

190
00:11:57,230 --> 00:12:01,730
Finally, and this is probably the more common way of saying this, but you could use else in case you

191
00:12:01,730 --> 00:12:03,120
want something else to happen.

192
00:12:03,140 --> 00:12:06,740
If there is no air here, it's up to you and depending on the situation.

193
00:12:06,980 --> 00:12:11,840
But typically, we're going to see things like this try and accept and then sometimes will add in a

194
00:12:11,840 --> 00:12:12,950
finally as well.

195
00:12:13,310 --> 00:12:17,540
And typically, the what is actually going to happen in terms of Django is you're going to attempt to

196
00:12:17,870 --> 00:12:22,760
visit a URL or modify some database instructions.

197
00:12:23,150 --> 00:12:29,030
And then if there is an error, we're going to report back to the user some sort of error message.

198
00:12:29,150 --> 00:12:33,020
Maybe we actually put it or embedded within the actual HTML code.

199
00:12:33,410 --> 00:12:39,470
And then finally, maybe we do something else, like take them to another page or refresh or something

200
00:12:39,470 --> 00:12:39,890
like that.

201
00:12:40,190 --> 00:12:42,650
That's what we're going to see once we know enough Django to actually do this.

202
00:12:42,860 --> 00:12:43,970
But that's again.

203
00:12:43,970 --> 00:12:48,710
Typically, what happens is the user tries to do something that are web sites not compatible with or

204
00:12:48,710 --> 00:12:50,030
it's a mistake on the user's part.

205
00:12:50,330 --> 00:12:52,100
And then we say, Hey, report back to the user.

206
00:12:52,100 --> 00:12:56,330
You got some sort of error here, maybe double check what you're looking for and then take them to another

207
00:12:56,330 --> 00:12:57,590
page or refresh.

208
00:12:57,920 --> 00:13:00,080
Keep in mind, finally, it's technically optional here.

209
00:13:00,260 --> 00:13:01,760
It will just work with a try.

210
00:13:01,760 --> 00:13:03,770
Except OK, that's it.

211
00:13:03,920 --> 00:13:04,970
I'll see you at the next lecture.

