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

Talk Python's Mastodon Michael Kennedy's Mastodon