Python 3, an Illustrated Tour Transcripts
Chapter: Language syntax
Lecture: Walk-through: Exceptions

Login or purchase this course to watch this video and the rest of the course contents.
0:01 In this video, we're going to look at exception test, open that up in an editor. Let's read about the problem, the first part says
0:08 define a customized exception color error that subclasses runtime error. This is pretty straightforward, we just make a new class
0:17 called color error and we're going to subclass runtime error. We don't have to do anything else, we can just pass for the body of that.
0:24 The reason why one might want to define their own class of exceptions is so that they can handle those in a special way in their application
0:32 and it makes looking for them and their application a lot easier.
0:36 The next part says create a function err_wrap that takes a function fn *args and **kwargs.
0:43 It should call and return the result of invoking the function with the arguments and the keyword arguments
0:48 if an exception is raised it will use the raise from exception chaining to wrap the error with a color error.
0:55 Okay, so this is taking advantage of higher order functions in Python and we can pass functions in as parameters to other functions
1:02 we're going to say def err_wrap and it's going to take a function. It's going to take variable positional arguments,
1:08 so *args and **kwargs and we're going to return the result of calling function *args with **kwargs here, but we don't just want to return this
1:19 we want to wrap it and make sure that if there is an exception in here that we catch that exception, so I'm just going to say try except exception as e
1:28 and then if I actually got an exception in here rather than raising that exception I'm going to wrap it. So I'm going to say raise color error from e,
1:38 what that will do is it will wrap color error from e and there will be a context in there that will point to the original exception
1:46 so we can run this and see if it works. It looks like it works, let's take a brief moment to look at the code and see what it is actually testing.
1:59 Here we're calling err wrap with the lambda that simply adds to numbers and it asserts that that result works.
2:06 Here we're calling err wrap with a lambda that divides twonumbers and it's doing division by 0 and it's asserting the it raises a color error
2:14 and then later on the context, the context manager object here has a value attribute which is the actual error
2:22 and on that there is a context which is zero division error. So the real error was the zero division error, but it got wrapped with color error.
2:31 Same thing down here below. We're making a function called raise 2 that just raises a key error,
2:36 and we're calling raise 2 and we're asserting that color error is raised but inside the wrapped context is key error,
2:45 so hopefully this gives you some insight into how to do wrapping in Python 3. This will help finding errors a little bit easier
2:55 and you can make them specific to your code if you'd like to.


Talk Python's Mastodon Michael Kennedy's Mastodon