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
0:03 and then catching errors that may result
0:05 from running that code using try except blocks.
0:08 So, here's some code that handles two specific errors
0:11 through a whole bunch of our program.
0:13 We run basically the entire program
0:15 and if ever we get a keyboard interrupt
0:17 that's control C.
0:18 It says let's print out hey, you're out of here
0:20 it looks like you got a run.
0:21 No problem, goodbye.
0:23 And if the rolls dot JSON file is malformed
0:26 we want to catch that error
0:28 and say there's a problem with the rolls dot JSON file
0:30 here are the details.
0:31 The way we do that is we say try:
0:34 and into that try block, we get into the block bind ending.
0:37 We have all the stuff that we think might cause a problem
0:39 and then we except.
0:41 Here's one error type, except.
0:42 Here's another error type
0:43 and Python will automatically say
0:45 if we encounter an exception
0:47 we're going to go and look at the various error types
0:50 and see which one matches.
0:51 It's important to know
0:52 that it's going to go down that list
0:53 and the first one that matches
0:55 it's going to go with that.
0:56 So if you have a very general thing like except exception
0:59 and then except JSON decode error
1:02 which is also an exception
1:03 it's like squares and rectangle sort of thing.
1:05 It's going to stop on the first, more generic one.
1:08 So you got to put the most specific stuff first
1:10 and the most general stuff last.
1:12 That's why I had that except exception as x
1:14 at the very end of my example before.
1:17 You can declare a variable, JE
1:19 and use it to get more details
1:20 or, if you don't care about that
1:22 you don't want to do anything with the details
1:23 just catch the exception type itself.