Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 10: Movie Search App
Lecture: Using try except to handle specific errors
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now we have our error handling in place, our app can fail a search
0:04
either because the network is down or for other reasons,
0:06
and we do catch it, but we have no information about it here,
0:10
it just says hey, that didn't work, sorry, something like that,
0:14
so we can add a little bit of extra information here to carry that error over,
0:20
and we can work with it, so we could say except exception as
0:25
and then we name a variable, the format is a little bit like context manager,
0:28
so except exception as x, and so this x is going to have all the details,
0:33
let's just say it like this, so now we can print this out
0:36
and we can see what the details are, so if we come over here
0:39
and turn off the wi-fi, I'll search for network— boom, that didn't work
0:44
http connection pool, let's see we got some kind of error
0:49
we'll figure out what that is in a second,
0:51
but how do you think users are going to feel about this error,
0:54
they are going to love it, they think this is a great error message
0:57
that really tells them what's going on—
0:59
no, they're going to hate it, so we need to do something a little more specific,
1:03
the first thing I want to look at is just what is the type really quick of that error,
1:07
the actual type, so the type is a request.exception.connection_error
1:14
so let's put this back for a second, and go up here to the top and import that;
1:22
ok, so now we'll be able to over here and we can have multiple except blocks
1:26
so watch this, we come up here and we create another one
1:29
we can say request.exception.connection_error
1:32
as this should be named differently, say ce, something like that
1:37
so for this one, we could change this and we could say
1:40
something to the effect of error, your network is down,
1:44
or host unreachable or something like that.
1:47
Now, we could put in details here,
1:50
but really I feel like that message already carries over enough of the information,
1:54
so let's just try this again with the network off,
1:56
I search for J — error, your network is down;
2:00
notice it didn't run this 'Yikes, that didn't work',
2:03
the way it works is we do the try block and if there is an error
2:06
Python will start looking from top to bottom to say
2:10
the thing that is the error is it one of these types,
2:13
the very first one that matches it's going to go with that.
2:17
So the ce thing, this is also an exception
2:20
because this is a base type in terms of inheritance,
2:23
it does match this one, so the first one it finds it stops and it uses that
2:27
so this means that you need the most specific ones first
2:31
and these more general ones have to go at the end
2:34
because it's just going to go top to bottom and stop as soon as it finds one.
2:36
Now notice here we're using the x and we're putting the details here
2:39
but in this one, we're not, like we're not actually using the ce
2:43
and PyCharm has it grayed, so we can omit this like so.
2:46
Now there's another problem, let me put that back just for a second
2:49
so I can show you the details, ok let's run this,
2:52
now when the wi-fi was off, we had a particular problem,
2:55
like if I do this it crashes, but let's turn the wi-fi on.
2:59
Now if I search for 'runner', it should work,
3:01
but if I search for nothing, if I just hit enter, it crashes, Yikes, that didn't work
3:05
client 404, this was not found, what is going on here?
3:09
So if you go look at this, notice the url structure has to have an api search
3:15
and then the keyword, if the keyword's not there,
3:18
you just get 404 because hey, I don't know what to do with that.
3:21
All right, so that is a problem, we need to somehow take that error
3:25
which was not a connection error, it was a different type, it was,
3:29
I think some sort of raise for status thing, yeah http error, more general one.
3:38
What we want to do is we want to actually go over to this movie service here
3:42
and say not even let them try to call this, like why are we doing this,
3:45
if this data is invalid we should check that,
3:48
so we can come over here and we can do this,
3:50
we can say if not search text, so if it's completely like if it's equal to none,
3:55
we want to test this or, not search text.strip
4:00
so if they just put like space or new line or something like that;
4:03
if that's the case, we want to communicate an error back to them as well,
4:06
we don't even want to let this code run, we just want to say no that failed.
4:10
Now we could return false, but that's very tricky to check,
4:14
how do you indicate empty result versus false, and things like that,
4:17
so what we should use here, just like request
4:20
some things like that are, we should use exceptions.
4:23
In this case, we want to create an exception, not catch one,
4:27
so we'll say raise, and probably the best thing to say is
4:30
value error, something like search text is required, something to that effect, okay.
4:35
Now if we run our code, we have our network on,
4:38
and if we search for 'cats', we get results,
4:42
if we search for nothing we get Yikes that didn't work value error,
4:46
so let's go back and add another specific error message for value error,
4:51
so we go over here, say value error
4:55
and here we'll say something to the effect of search text is required
5:00
let's clean this up a little bit, like this.
5:04
Excellent, so if there is a value error, we give them this message,
5:09
if there's a network error, we give them this message,
5:12
otherwise we'll just say unexpected error, details like that
5:18
and let's not put the type, there we go,
5:21
so I feel like this is pretty good, let's give it a try—
5:25
network is on, we search for 'runner', do we get something, yes,
5:29
we search for nothing— error, search text is required,
5:32
network turned off, what do we get— error, your network is down;
5:37
search for nothing— error, search text is required,
5:40
turn the network back on, last time—
5:44
and we're back in business, how about that?
5:47
We're able to use the type of error to actually catch it,
5:51
and handle different cases differently,
5:54
and we're also able to raise these errors
5:57
and indicate some kind of problem
5:59
before it cascades into something non obvious
6:02
like the http error, like why does that mean that the search text is empty,
6:05
let's be more proactive and make that really obvious
6:08
in a way that we can deal with it specifically.
6:11
Alright, so that's error handling in Python.