Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Error Messages
Lecture: Demo: Adding Notes to Exceptions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright let's explore these exception notes at any file, EX notes. And again I want to start with a somewhat reasonable,
0:12 slightly realistic set of codes. So let me drop a bit of code in here for us.
0:17 Now we've got a DB exception class that derives from exception and adds nothing special to it.
0:23 The DB connection exception derives from that. Again, just taking whatever the base exception gives us.
0:30 And here we've got our connect to DB and you can see our test if the server is in the connection
0:35 string and the server specified. That's a problem. If the port is in the connection string and the ports
0:41 specified, that's a problem. Otherwise port connect to the database again.
0:47 Just a sample. But if there we give this conflicting information there's gonna be a problem down here.
0:54 We've got this Mongo connection there that passes the user name, password and database is not a real MongoDB connection.
1:03 Just roll with it for a second. We've got this more troublesome one that passes the ports,
1:10 we have a different port here that we're gonna pass one call it. Okay so here's our setup app. This is the one that might be causing us trouble.
1:17 So let's go ahead and run this code now this doesn't have any of those notes added yet.
1:22 So we've run it and it says awesome. You connected to the database and you can see it's a men
1:27 appended onto the connection string. The server and import but if we go back and say let's try to connect
1:34 with this conflicting one bam error error. Starting application the database,
1:41 there's been a database connection exception. The connection string is malformed. Why is it now malformed? What's wrong with it?
1:48 Okay well this function presumably this is my working theory of this example.
1:56 Is that this function knows when it gets that database connection error of that type. But that means the the database connection string is messed up.
2:07 No, I know it's weird because it's also creating the connection string.
2:11 But let's just say that this is the section of the code that knows if we try and we say except DbConnection exception as dbe.
2:21 Then we can go over here and we can say dbe.add note doesn't look like PyCharm thinks that's there. Does it stick with me for a second.
2:35 I'll tell you what's going on. So first we're gonna say note you cannot specify the server or port in
2:42 both the connection string and explicitly and we want to add that other one. Amend... note, Amend the connection string and try again.
2:51 Now look down here, we're catching this exception here and we're printing out some error details like what type of
2:58 error and what is the message? Let's run this app ready. Okay that doesn't seem great now does it because when we caught the exception here we ate it.
3:10 So that's why I think it didn't crash. So we need to say raise raise like this.
3:15 Let it keep going or stopping for a minute we're adding some details and we're letting it go right there. Try again. Did not start in the right way.
3:26 DB connection exception connection string is malformed. Wait, we added the notes where the notes go. Well if this was a full on crash,
3:37 you would actually see the notes. So let's try to run that outside of this. Try except block here. You can see those three details right there.
3:48 Though we've got the DB connection exception this and then we have these notes,
3:54 whether we want to actually explicitly call them notes, maybe we want to put them in like this.
3:58 However, that string appears there that just gets echoed below the air message.
4:03 Okay, no additional information. There's no clue that that is the notes in the error message.
4:09 But we can also work with us if we catch it and we can see the notes for ourselves.
4:14 So for example, we could come over and say for n in x.notes dunder notes Again,
4:23 PyCharm says they're not there. Give me a minute and we'll make sense of that. We'll print the Sad note will say there's a note colon,
4:30 whatever that note is now we run it and says, look, there's these extra notes or you could say something like we could say print,
4:54 we could do something like this where we say, hey look,
4:57 we found some notes. There are two notes associated with this exception and here's what they are.
5:02 So you can do whatever type of reporting around it you like the key.
5:06 Take away. The key idea is there's some part of your code that maybe can't handle the exception,
5:12 right? We need to let the caller of setup app know this broke,
5:17 something went bad. Here's the exception, but we also have more information about what went wrong and a little
5:24 contrived. But we know that you can't specify both the server and port explicitly.
5:29 If we see this exception, that's what happened. So we can upgrade the error message.


Talk Python's Mastodon Michael Kennedy's Mastodon