#100DaysOfCode in Python Transcripts
Chapter: Days 25-27: Error handling
Lecture: Demo: Error types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we've stopped the errors from crashing our program
0:02 but we can't do anything meaningful,
0:04 we can only say, you know, that didn't work, sorry.
0:08 Let's be a little more prescriptive here.
0:11 So up the top,
0:12 we need to work with the various exception types,
0:15 so we have to import some stuff.
0:16 So we'll say request.exceptions down here.
0:20 Now in this area, we want to catch
0:22 the different types of problems that can occur,
0:25 and the way we do that is we say except:
0:27 and then we say 'different types',
0:29 remember when we looked at that trace back,
0:31 the first thing is the category, or the type of error,
0:33 and the second part after the colon was the description,
0:36 just more details.
0:38 So what we need to do is,
0:39 we could have potentially multiple ones of these,
0:41 we'll say this, we'll say,
0:43 request.exceptions.connectionerror, okay?
0:48 We be able to just go like this,
0:50 and just do a little print here and say,
0:54 so maybe we'll say something like,
0:55 'couldn't find the server, check your network connection',
0:58 make this really obvious like that,
1:01 and print that out here.
1:02 Now, it could be if this was like a web browser
1:05 they could've typed it in wrong,
1:06 but we've hard-coded the URL, we know that that's correct,
1:09 so if they can't connect it's not the server doesn't exist,
1:12 it's really that there's some problem
1:14 getting to this one known server.
1:16 So let's try this again with my internet off.
1:22 We'll come down here and search for test.
1:23 'Could not find server, check your internet connection.'
1:26 Oh, how cool, that's way more helpful,
1:28 we know exactly what went wrong
1:30 when we want to handle that problem here.
1:33 Now the network is back and we have another problem.
1:36 If we enter no search term it's going to freak out
1:39 and throw what's called a ValueError, and say,
1:41 'hey that didn't work.'
1:43 But all we get is, oh, that didn't work.
1:45 So we could do a little bit better here,
1:47 we could say,
1:48 put this core exception here,
1:51 and we could put some details like so.
1:54 This is kind of going to be our fallback,
1:56 we don't really know what's going on,
1:57 so we're going to try this.
1:58 Let's try again.
2:00 Oh, that didn't work, 'you must specify a search term',
2:03 and if you're just debugging it to try to figure out
2:05 what's going on here you'd say,
2:06 what is the actual thing that I'm getting here?
2:08 What is this actual error?
2:11 It's a ValueError.
2:12 Okay.
2:13 So now let's go add an except clause for that.
2:19 Now notice this is gray because we're not using it,
2:21 and when we're not, we can just leave it off like,
2:24 as we did with the request here.
2:26 So we don't need any details, so now,
2:28 try to run this with nothing,
2:29 'error, you must specify a search term.'
2:31 Now we can really tell the user what's going on.
2:34 We have a few more errors
2:35 that we could potentially deal with,
2:38 I wouldn't leave this here like that,
2:43 and these other errors are just kind of random stuff
2:45 that I threw in there to make the program crash.
2:47 One is a StopIteration,
2:49 and the other is the one that you saw at the opening,
2:50 which is a TypeError I believe.
2:53 So we'll just let those fall through here
2:54 because there's not anything we can particularly do,
2:57 they're just sort of random noise in the system
2:59 to make sure you get some interesting crashes.
3:01 These have real fixes,
3:03 so we have a special message for them.
3:06 The final important thing to see here
3:07 in this whole try except block,
3:10 is the order in which we specify the errors.
3:13 If we change this around
3:15 and we put this up here at the front,
3:17 PyCharm's probably going to freak out
3:18 and say, "no no no, you'll never be getting to these."
3:22 If we have something general,
3:24 and this is a derivative of exception,
3:27 this is not going to work.
3:28 It's actually just going to stop.
3:29 The way it works is, it runs,
3:31 if there's an error it just looks, goes, 'is the error this type, is the error this type,
3:34 is the error that type?'
3:35 And, if it comes along here,
3:37 this is going to catch almost everything,
3:39 and so, even though we have this,
3:41 you'll see if I run it and I hit enter,
3:43 that didn't work.
3:44 I mean, we still did sort of print this out,
3:45 but it's not letting us get to the part where we expect it.
3:51 So it's super important that this goes
3:52 from most specific to most general.
3:55 So, here we have it;
3:57 we've figured out what types of errors
3:58 our program might throw,
4:00 and then we can handle them independently based on the type.