#100DaysOfCode in Python Transcripts
Chapter: Days 25-27: Error handling
Lecture: Demo: try-except blocks
0:00 We've seen that, calling this API, may raise errors.
0:04 And notice the WiFi being off,
0:06 pretty much anything we search for
0:08 is going to result in an error.
0:10 So, we're going to turn the WiFi back on.
0:12 But before we do, let's convert this full on crash
0:14 to something we can catch, maybe log,
0:16 send a message to the user.
0:17 Or we could try again, who knows?
0:19 Here's how error handling in Python works.
0:23 What we're going to do is,
0:25 there's a couple options.
0:26 We could treat this as a separate section.
0:29 Let's say, this part that actually interacts
0:31 with the data does the request.
0:33 We're going to treat this as a block
0:34 that should either work,
0:36 or not work here.
0:37 So, we can come up here and type the word try:
0:41 And indent that into the block.
0:43 Then if something might go wrong,
0:44 we'll say, except:
0:45 And I'll say, print
0:47 Oh, that didn't work.
0:48 And we'll just print that out.
0:51 Now Python says this is a little aggressive,
0:54 it's too broad, but, that's okay.
0:56 This is going to work for now,
0:57 and then we'll refine this a little bit.
1:00 So now if I run it,
1:02 oh that didn't work.
1:03 It's a big bad crash, um, we just say,
1:05 "You know, that didn't work."
1:06 We could've logged it.
1:07 We could actually loop around and try again.
1:09 All sorts of things.
1:10 But at least we're giving proper feedback
1:12 to system here.
1:15 Let's turn this back on.
1:18 WiFi's back.
1:19 Let's try again.
1:20 Let's search for capital again.
1:22 Boom, it's working.
1:24 Here's how the flow goes.
1:25 We're going to run every function here.
1:28 And if it succeeds, it's just going to run
1:31 from here to there and then skip
1:33 over this except block.
1:34 But if at any step something goes wrong, like,
1:36 suppose, say right here, this title doesn't exist,
1:39 and it's going to be an error
1:40 the first time through or something.
1:41 It's going to immediately stop what it's doing
1:44 and jump down here.
1:45 If this throws an error,
1:46 we're not even going to run this prank code.
1:48 We're just going to jump straight from here
1:49 to there to deal with it.
1:51 So, we can try some stuff, didn't find anything.
1:54 Eventually, we'll run into these random errors.
1:57 That one didn't work. So, this is great, but what didn't work?
2:01 Wouldn't it be nice to know what is going on?
2:04 It turns out there's three or four categories of errors.
2:07 One of which is the network is down.
2:09 But there's actually others.
2:10 So, we're going to adjust this
2:13 to handle these particular errors.
2:15 So, here's how we can stop the errors
2:17 from crashing our program.
2:19 But let's see how we can actually deal with the errors
2:21 in a more fine grained way.
2:23 One message for say, network errors.
2:25 Another message for say,
2:27 if you didn't input any keyword to search for.