Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 10: Movie Search App
Lecture: Catching errors with try except

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are in our program again, and it looks like everything's fine if I go look for 'runner' we get some results,
0:07 but remember, this is on the internet, so what happens if say or wi-fi is down, now let's try it again, if I come over here and I search for 'runner'
0:16 this is the exact same thing, that's bad, look at this, connection error something to do with requests,
0:23 could not connect to that url, things like this. Our app is fully crashing and notice, it exited with code 1,
0:30 usually code zero is good, anything not zero- not good. All right, so something is going wrong here,
0:36 and it's because when we go here and we do this line right there, we don't get access, we can't reach the url,
0:43 I'm not sure which line it is but if it's 12 or 13, but check this out, if you click here, go and find some of your code
0:52 and you click there it'll actually take you to the line, so it's actually line 12 not even the raise for status
0:57 like it couldn't get to the point where it got a status, okay? So how do we convert this from crashing our app
1:03 to just giving a message to the user like hey that didn't work. Let's go over here, and the way we're going to do this is
1:09 we're going to use the exception handling in Python, now what we're going to do is we're going to go find all the code that we want to run
1:16 and we're going to indent it, and we want to come up here and we're going to put it into what's called a try block,
1:21 so we want to say try to run all of this code and if it doesn't, if there's some kind of error, we can print 'Yikes, that didn't work'.
1:28 Ok let's run this again, and now the network is still off and I search for 'runner' 'Yikes, that didn't work', search for 'boo' it didn't work,
1:38 turn on the network— and now, if I search for 'runner' hey, it's back, so our app is way more reliable,
1:46 it's not fully crashing, it's just saying that didn't really work, you want to try again, something like this, right, until we decide to exit.
1:54 So that's really cool, however, we're going to notice that there's some other interesting problems here,
2:01 one of the things that could be wrong is that the network is down, it could be a problem where those service doesn't respond
2:08 or we got bad data from the service, it could be other things, like for example watch this— so the network is on
2:16 and if I search for 'runner', it's great, if I search for nothing, 'Yikes, that didn't work', hmm I don't actually know why that didn't work,
2:26 well, I do, but there's nothing clear, there's nothing obvious about why that didn't work and you have to know a little bit about the service
2:32 to actually know why that didn't work. Even though the network is here, that also failed, but we're given exactly the same message back to the user
2:39 and in fact, if you look over here in PyCharm, it says this is too broad of an exception clause,
2:44 we're catching everything, in fact, we're even catching like control c trying to break out of the app and watch if I try to hit stop here—
2:51 it's going to just get this weird like skeletal face and say 'Yikes that didn't work',
2:58 you can't exit me basically, is what it is saying, I will live forever. So this is not really the best thing to be doing, we should be looking for both
3:08 different types of errors and reporting different messages as well as not catching like exit application type of exceptions or messages as well.


Talk Python's Mastodon Michael Kennedy's Mastodon