This lesson shows how to use
finally to execute sections of your code that should always run whether or not exceptions are encountered. You’ll also see how
finally can be used to clean up after executing your code as well as how it can be used to re-raise exceptions.
Cleaning Up After Errors
This lesson shows how to use
Other times when you’re handling exceptions, you’ll need to do some cleanup after the fact, so we’re going to quickly just stub out a class that needs some cleanup to be done. We’re going to create a class called
And we’re going to have that. Now we’re going to artificially raise an error in a function called
.send(). So we’re going to define a function called
.send(), so we’re going to try to send something along the line here.
It’s simply going to be
def send(), some data. And then we’re going to raise a
RuntimeError here. And when
.send() is called on this particular thing… I missed a colon there. When we call
.send(), it’ll cause an exception to happen, which will put it into this block. This will be printed.
So, you try something. So, let’s say you’re doing your work. Normally, you’d put the
.close() is probably a better name for that function.
Connection.close() here, we will then continue our execution of our program.
What you should do in a case like this is you’re not sure if whether or not it’ll except, but when it does, you want to make sure that things that you would have normally called on exit are still called. And that’s where the
finally block comes into play.
finally block will be run regardless of exception or not. It is responsible for things like cleanup or re-raising errors. You re-raise errors usually when you’re dealing with a larger system, which has its own set of errors that are understood by its maintainers.
I’ll put that in the screencast notes. But what those let you do is re-raise system-defined errors, which can then trigger a bunch of other things. You can also use the
finally block to roll back transactions.
That’s the type of thing that you’d put in a
finally block. In this particular case, we want to make sure our connection is closed, so we run
conn.close(). So we will create a
Connection, we will try to send something along that
RuntimeError will occur, this will be printed, we will then finally run
conn.close(), and then if we feel the need, we can then raise another type of exception which is a
So, you saw that the
RuntimeError occurred. You saw that we closed the connection that we needed to close, and then a
BaseException was raised. Now in a larger system, this would then again be caught by some monitoring portion, either Sentry, which is an excellent tool for monitoring web applications, or another system that deals with exceptions that happen in an application.
It keeps track of all the exceptions that were raised. If you simply want to re-raise the exception that brought you here, all you need to call is the
raise command without any attributes, and you’ll see that it’ll raise the
Become a Member to join the conversation.