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


Talk Python's Mastodon Michael Kennedy's Mastodon