Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Create a Custom Exception

00:00 I don’t like this AssertionError here. One thing that you can do, because this seems to be a pretty specific codebase that this person is working on, and you can always subclass the built-in Exception object and just give it a more descriptive name.

00:14 You don’t even have to do anything else for a custom exception, but you can just give it a descriptive name. In this case, that could be—we know it’s about "source" and "target" shouldn’t be the same—so the error could be

00:27 SourceEqualsTargetException or Error.

00:34 And I want to inherit from the built-in Exception object, and I don’t need need to do anything else in here. So I’m just putting a pass statement at the end.

00:44 But this allows me to pick this name and raise my custom exception here, the SourceEqualsTargetError, that also then has a descriptive message associated with it.

00:54 But you already learn a lot about what the point of this error is just from the name of the error really. So maybe this is not even necessary. Let’s take it out. Yeah.

01:05 So I have this SourceEqualsTargetErrorpass—or maybe a little better than just taking it out would be to put it in here as a docstring.

01:16 shouldn't be the same.

01:19 shouldn't be the same. Okay. What do you think about that? Nice. So when you run it again, then the SourceEqualsTargetError will be raised. Gets raised, yeah.

01:31 Okay. And you could pass in the string that I just took out here. You could keep this in there and then it would also appear as an additional context to the SourceEqualsTargetError. In the terminal, you can see that when I add the string then it also appears in the stack trace, but I don’t really think it’s necessary when we have this custom descriptive name of this exception. Yeah, so the reason why you changed the AssertionError to the SourceEqualsTargetError is not only that it’s more descriptive name-wise, but also that the error that was thrown wasn’t an AssertionError per se.

02:08 Exactly. In Python, generally it’s also okay to raise errors manually, but when you do so, you should know which error to raise. So you don’t want to just raise any error, but you want to be precise about what’s happening there.

02:23 And if you’re unsure about which Python exception you want to raise, then you actually can say, like, “Well, actually, the error that I want to raise is that the source doesn’t equal the target”—or no, that the source equals the target in this case.

02:36 So I create my own error and then it says exactly what it is, and I don’t have to look through the different exceptions that Python offers. I can just create my own error.

Become a Member to join the conversation.