Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Error Handling Improvements and except*
Lecture: Except* Demo

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's write some code that may or may not have multiple errors and you'll see what happens.
0:07 We'll just play with it. So I'm gonna add a new file here.
0:12 I'm gonna call this except star, again I want to start with some existing code and then play with it
0:19 so you can see what's happening. So let's look at this code that I dropped in here.
0:25 We've got Except star and we've got some async methods for logging and you'll see down here.
0:30 That's external API log and database. Just like the example from the slides before and we have different types
0:38 of exceptions. We have a DB exception and http exception of socket exception and others. So for example, this one can have an http exception.
0:49 The dB can have a socket exception. For example, the database server is turned off. Right. So what we're gonna do is we want to record that action
0:58 details not really specified what we want to record, but we're gonna use that as a task group to do these in parallel. Hey, we're in 3 11.
1:06 Let's use the 3 11 features. Right, awesome. So we're gonna kick these off and I've written them to be specifically unreliable.
1:16 Sometimes they will succeed. Sometimes will fail. Sometimes more than one will fail. Sometimes they all succeed. So what we want to do is we're,
1:25 we know it's some sketchy code we're working with. So we're gonna run it in a try except block.
1:31 Right now. We're just catching the exception and literally just calling Print X.
1:35 That's it. No additional details. Like looping over the notes when we did that before. All right. So we're gonna run main it's gonna run this record.
1:45 We'll do the task group to do sketchy things in parallel. There's an error. We're gonna print out what happens.
1:50 Let's try the login. Api file file worked awesome. DB work. But we don't see the API. worked. What we got is a un-handled errors in a task group.
2:03 Let's change this a little bit. So we print out two things, let's print out a type(x).dunder name and that all worked.
2:14 No errors. Super. Okay. Look what we got is an exception group. Have a task group that started it. There were multiple exceptions.
2:22 So we get, well an exception group this case there's one this message is 100% controlled by python.
2:30 3.11's exception group error. Okay. It's not something I did as you can see right there.
2:36 So that's what it looks like. Let's try to run it outside of a try. Except, that one worked. There we go. Maybe that's more what you expected.
2:48 So we try to run some code some things went wrong and then here's the exception group because two there are
2:55 two sub exceptions. One of them is there's an HTTP exception. No can API. And there's a DB exception.
3:02 No Can DB. Okay. So we get the trace backs for each of them and you could click over here and you would be able to see where that actually happened.
3:13 Remember we called record action to external API.
3:15 And record actually the database within the task group. So it's kind of a call stack relative to where the task group was. Okay so this is pretty cool.
3:28 What do you do with this? So in a sense this makes it like a little bit less useful Comment
3:35 that one out. If I want to capture an exception I just say well I'm gonna capture an exception group
3:40 and then I've got to traverse this and dive into it and figure out all the errors. I just made my life harder. Didn't it? Not fun.
3:50 Thanks asyncio thanks a lot. In fact let's replicate this.
3:56 Alright so I'm gonna comment out this old one. Leave it here for you to have in its pure form
4:01 And what we're gonna do is we're gonna catch the different exception types.
4:06 So normally you would say except db exception as db accept http exception as 'HE'.
4:20 For error. What are some other ones? We could do the file exception can't we?
4:26 We would say catch that as FE. And let's just actually I'll just print the name real quick
4:34 just so you can see are we hitting any of these do you think we'll see any of these. Right was any of these the top level exception that we ran into?
4:42 Well let's find out. I'll run a bunch of times exception group. There are two exceptions. See if there's just one. There,
4:52 there was just one. Which one? It was the database one. Well, we're catching up, we're catching the database exception right there.
5:00 But no no no. What we were thrown is an exception group that internally, if you were to inspect it, it had one of these database exceptions in it.
5:11 So check this out. We can use a different syntax, we can say except star dB exception. And let's right now,
5:22 just the exception here like that, not the best formatting but it's going to work. This will be an accept star things that don't match.
5:33 So watch what happens when we run it now there's a database on handled error and task group.
5:42 Okay. And HTTP exception in unhandled error in task group but there were actually two errors in the task group
5:50 Look at that 2 Except classes ran. That's what the except star does. It says find all of the http exceptions and things that are derived from that.
6:00 And run run, send me a specific task group for those and the same for the database exception.
6:08 So let's see what we can do to get a little more details here. So we're gonna go over here and say for e in db.exceptions.
6:19 And I want to print dB exception will make this little f string here.
6:37 We want to print the e There we go. We're gonna go through all those inner exceptions,
6:41 there might just be one, but they're theoretically as far as python's concerned,
6:45 there could be multiple dB exceptions that have happened. Let's try this again. So http just comes out like this. Let's find a database exception.
6:55 There it is. Database exception. We got an error with dB. No Can db, if you care about the specific details,
7:04 you can loop through it like this. Alternatively, we could just say something like this. There was an http exception. That's an error with API.
7:13 File exception. There's an error with a file right again,
7:16 notice now we're getting meaningful messages if we get multiple of these back in the database version,
7:23 we get specific details about what that is and for the others we just say the type of error is enough
7:32 information for us. Here's what went wrong now over here we ran it one more time.
7:38 And one of these was some kind of exception that didn't fit into the HTTP. File or the dB exception.
7:48 It was some other kind and it fell into this general except star exception here.
7:53 So this might be another case down here where we want to print will say something like this.
8:00 So we could say seven or three or whatever additional errors and then print out those details. So let's try that one more time. I'm gonna run it upon.
8:08 Look, we hit all three except for the final catch all. Here we go. We have one additional error. No can db.
8:15 I believe that's a socket exception. In addition, if we can't connect, we get the socket exception. If there's something internally wrong with the DB,
8:22 we get the DB exception so potentially we wouldn't want to catch that up here in that section there.
8:32 But check this out pretty wild. Huh? We can remove these unused variables here because we're not using them
8:39 but we have this except star syntax that will capture one of these exception groups and partition it into basically
8:48 by matching all those categories. Run multiple of these and it gives us one of these exception groups.
8:54 We got to dive in deeper to figure out what's happening. So if you're doing a group of operations and more than one might fail.
9:01 Here's a really cool way to catch those exceptions and but standard python,
9:07 like error handling around what turns out to be a pretty tricky case of figuring out what's happened when you get multiple exceptions except star and exception groups.


Talk Python's Mastodon Michael Kennedy's Mastodon