#100DaysOfCode in Python Transcripts
Chapter: Days 25-27: Error handling
Lecture: Concepts: Error handling and exceptions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's quickly review the concepts of try and except blocks in Python. Python's primary error handling style is what's called it's easier to ask
0:11 for forgiveness than permission. As opposed to, say, C style of look before you leap. In C you check, check, check, check, check,
0:19 and then you just try to do the thing and hope it works. Typically what happens when it doesn't work
0:24 is either you get a false sort of return value there or just the program just goes away, it's really bad. Python is a little bit safer in that
0:31 it's more carefully captures up the errors and converts them to exceptions. So if it's going to do that anyway, let's just try and make it work.
0:39 And if it doesn't work, well, we'll catch it and deal with it in that case. So it's kind of an optimistic way of handling errors.
0:45 so what we're going to do is we're going to say try and do all the stuff, and we're going to hope that it all just works
0:50 and kind of assume that it will just go through that block. But if it doesn't, we're going to drop into one of the specific error handling sections.
0:57 Here we have two possible errors, really one that we're dealing with, and the rest is kind of a catch all.
1:02 So we're saying except connection error as CE, and then we're going to deal with that. And in this case we might need to look inside the error
1:10 to see, well, was there a DNS problem, is there a network problem, did it not respond, things like that,
1:16 did we get a 500 back from the server, all kinds of things. So this connection error, we're going to catch and deal with that
1:22 and then we do this more general except block where here's something we maybe didn't think of, we're going to catch that and at
1:28 least try to somewhat not crash. As we talked about before, the order matters. Most specific goes first, most general last.
1:35 If you get that order wrong, you'll never get to your specific errors. So most specific, most general. This is error handling in Python.

Talk Python's Mastodon Michael Kennedy's Mastodon