Python for Absolute Beginners Transcripts
Chapter: Error handling
Lecture: Concept: Python's exception handling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review catching, prying to run code and then catching errors that may result from running that code using try except blocks.
0:09 So, here's some code that handles two specific errors through a whole bunch of our program. We run basically the entire program
0:16 and if ever we get a keyboard interrupt that's control C. It says let's print out hey, you're out of here it looks like you got a run.
0:22 No problem, goodbye. And if the rolls dot JSON file is malformed we want to catch that error and say there's a problem with the rolls dot JSON file
0:31 here are the details. The way we do that is we say try: and into that try block, we get into the block bind ending.
0:38 We have all the stuff that we think might cause a problem and then we except. Here's one error type, except. Here's another error type
0:44 and Python will automatically say if we encounter an exception we're going to go and look at the various error types and see which one matches.
0:52 It's important to know that it's going to go down that list and the first one that matches it's going to go with that.
0:57 So if you have a very general thing like except exception and then except JSON decode error which is also an exception
1:04 it's like squares and rectangle sort of thing. It's going to stop on the first, more generic one. So you got to put the most specific stuff first
1:11 and the most general stuff last. That's why I had that except exception as x at the very end of my example before. You can declare a variable, JE
1:20 and use it to get more details or, if you don't care about that you don't want to do anything with the details just catch the exception type itself.


Talk Python's Mastodon Michael Kennedy's Mastodon