Python for Absolute Beginners Transcripts
Chapter: Error handling
Lecture: Invalid file formats

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, that was pretty excellent.
0:01 We've got the error handling, I think
0:03 pretty solid around this text input thing here.
0:07 However, remember this rules.json that we created?
0:10 How cool was it?
0:11 One of the selling points
0:12 that I told you that would make this neat
0:14 is it would allow for people who are not programmers
0:16 to go and add in their own thing right here.
0:19 But, people being people
0:21 and especially non-developers messing with code-like files
0:25 things can go wrong. So for example, what if minor mistake on line 13
0:30 no coincidence there, what an unlucky line that is.
0:33 Minor mistake.
0:34 Something is not right here.
0:36 So, what's wrong is of course we need that comma.
0:38 If you had a proper tool like PyCharm
0:40 it would tell you you need a comma.
0:42 But, most people probably won't edit it
0:44 with something like that
0:45 who are just fooling with this thing.
0:46 So let's try to run it again.
0:48 Now is it going to work if I try to run this?
0:54 Well, that's amazing, not at all.
0:56 So it showed our little header, cool.
0:58 And, and then it didn't even start.
1:00 It didn't say something was wrong, it just, boom, exploded.
1:03 Again, you have the error.
1:05 We want to handle that error
1:06 so let's grab the type of error that it is.
1:08 The type of exception.
1:09 And it says details about this
1:11 and we can deal with it or not.
1:12 Incorporate that or not.
1:14 Let's go over here and say we're going to deal with it
1:16 when this goes wrong.
1:17 And we're not needing that in this example.
1:19 Now, could put this just in load rolls
1:21 but I kind of want, when it runs down here
1:23 if something goes wrong with loading the rolls
1:25 we can't play, right? It's done.
1:28 The game is fully broken.
1:29 So what I'm going to do
1:31 is I'm going to put this whole thing in here into a try.
1:34 Do an indent.
1:35 Except, and we could just put print error starting for now.
1:40 And we try to run it again.
1:42 Error starting. It's not super helpful for us.
1:45 Let's put some color in this.
1:47 From light red. Like that.
1:53 Well, that's better.
1:55 It's a red error starting, why?
1:57 What's wrong with it? Well let's be a little bit more specific.
1:59 Let's fit it in here we're going to have an except a block.
2:03 You can have more than one of these
2:04 we're going to have that json.decoder.json error
2:07 and we can just start for now like that, hit Print.
2:13 Error the format and the file rolls.json is invalid JSON.
2:19 How 'about that.
2:20 Now if we try to run it, it's going to come down
2:22 and it's going to start looking through all the things
2:24 listed here and saying which one matches the error
2:27 that is a JSON error?
2:28 Well, this one.
2:29 And we try to run it, error
2:30 the file rolls.json is invalid JSON.
2:35 Cool, well, it'd be cool to give them
2:36 a little more detail here.
2:38 So we could come over here and actually put the message.
2:42 We make that an F string
2:43 and we declare a variable as je for JSON error.
2:47 We could print that out and let's try it again.
2:49 Here we go, error, the file rolls.json is invalid.
2:53 We're expecting a comma on line 14.
2:55 That's cool because that means
2:56 that people who are messing with it
2:58 it now tells them not just, oh, it's broken
3:00 no no no, go to line 14
3:03 and we're expecting something, right here.
3:06 Well, what's right before that that's missing?
3:08 There we go, now if we run it, ah, it's working again.
3:10 Super cool, right?
3:11 So we're able to deal with that.
3:13 Let's actually look at one more thing.
3:15 Let me run it again. I want to run it outside of PyCharm.
3:19 There like that, and I want it to wrap around
3:21 just so you can see.
3:22 Easier to copy this way.
3:23 So let's go run that out here, like this.
3:27 We could just type this again, My name is Michael
3:30 and if I hit that, error starting.
3:32 Hm, that's weird.
3:33 So let's go back and actually
3:35 maybe even take that error away over here.
3:37 Let's see.
3:38 This error starting bit.
3:41 So now, if we run it again, My name is Michael
3:44 and I try to exit out, I get this
3:46 oh, it's all broken.
3:48 Keyboard interrupt.
3:49 Again, that's the error type.
3:51 So let's go and actually go here
3:53 and say we're going to catch keyboard interrupt as well.
3:57 We don't need to create a variable that holds this.
4:00 There's nothing really to do.
4:01 And it's not even an error.
4:02 We'll just do, right, they just are trying to exit
4:04 but that's the way Python does it.
4:07 We'll go with a light cyan, you got to run, okay
4:12 see you next time.
4:14 All right, let's try this again
4:16 see if we can do better than this look here.
4:17 Again, My name is Michael, oh actually, I want out
4:20 Ctrl+C, you got to run?
4:22 Okay, see you next time.
4:23 Because we are on an input line, yeah the print put it here
4:27 so I kind of would like to do a print before all of these.
4:30 Let's just do prints here and go on.
4:33 Oh, one more. One more test.
4:36 We'll see, got to run?
4:38 Okay, see you next time.
4:39 No problem.
4:40 And color's even back.
4:42 Yeah, this is great.
4:43 So we're actually handling these errors
4:45 at the different levels, right?
4:47 So over here we call into rolls, go down
4:51 rolls calls open, that worked, and it calls load
4:55 but load looks at the file and says
4:57 Oh no, something is really wrong here.
5:00 One more thing we might want to check for
5:02 if we're letting people mess with the rolls
5:05 is file not found error.
5:09 And we could just say error, rolls file, rolls file.
5:17 And then here, let's put FE for that.
5:20 So, this might occur, this file not found might occur
5:24 if we, let's go in the right directory
5:27 we're over here, and we renamed rolls.
5:37 Now it's gone, and we try to play our game.
5:42 Rolls file not found
5:43 no such file or directory
5:45 and it tells them, Here's a huge great long file
5:47 that we were looking for that's not there.
5:50 So let's put it back.
5:53 Now we should be able to play again.
5:54 But we gave 'em a nice message instead of again
5:56 it just crashing wide open, right?
5:59 Now, My name is Michael, let's try around here.
6:01 Let's go for seven, yeah, no, you can't do that.
6:03 Let's go for seven, throw some water
6:05 just going to start throwing water on this thing.
6:07 Oh yeah, tied, here it takes the round.
6:09 Again.
6:10 How many times has this thing beat me?
6:12 Well I guess we didn't copy over the leader board.
6:14 But it's beaten me a lot.
6:16 Ctrl+C.
6:17 I'm out of here, I don't want to play anymore.
6:18 You got to run?
6:19 Okay, see you next time.
6:20 So hopefully that gives you a sense
6:22 of how working with error handling in Python
6:25 using try and except blocks, where we try to do something
6:29 and we have except blocks for the specific errors
6:32 that we're trying to deal with.
6:33 One more thing we can have down here
6:35 we can have except exception, like that.
6:37 And then down here we can just print as X
6:40 something like that.
6:42 And we can put escape with errors
6:45 just error with the game or something like that.
6:48 Unknown error.
6:50 X in there and make it an F string, there we go.
6:53 All right, looks like that's good.
6:55 So that'll catch some unknown error
6:57 that we might not have expected
6:59 and at least it'll print out a message
7:00 instead of flat-out crashing.
7:02 Now here we have it.
7:03 This is a lot of stuff but it's just in our main file.
7:05 It's kind of like is the final catch-all.
7:08 We also had some in our play game
7:11 where we were actually getting the input at a lower level.
7:14 So you can have it at different levels of your program.