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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Cleaning Up With finally

00:00 In this lesson, you will learn about yet another keyword that you can add to the tryexcept block, which is called finally.

00:07 It’s going to be the last keyword that you learn about here. This keyword opens up a code block that is going to always run. It doesn’t matter whether or not an exception got raised, it’s just always going to execute this code afterwards.

00:21 And this is going to be important, for example, if you want to close a file stream or just for logging, for example. There’s a couple of scenarios where you would want to definitely run a piece of code as part this tryexcept block at the end of it, no matter whether an exception got raised and caught or not.

00:38 And since this whole project here that you were building mostly consists of logging, let’s add another logging in here. I’m going to say finally:

00:49 as part of the outermost tryexcept.

00:53 I’m going to say finally: print("Cleaning up despite any exceptions."). So this is going to run no matter what, and then this "happens after".

01:02 Let’s try it out with these two scenarios of a failing and of a working run of this code. First, let’s have the working one from before. If I run this, you see the important macOS work gets done.

01:18 The else block executes, no problems. Then the logging file is attempted to be read. It doesn’t exist, you catch the exception and print out something.

01:27 Finally, inside of the finally block, this executes and it cleans up despite any exceptions. So you could be, for example, closing a file stream that you opened up earlier somewhere in your code.

01:39 Now let’s take a look at what happens with the Linux interaction. You know that this is going to cause an error on my system. So if I run this instead, the output is much shorter.

01:52 What happens is that the first OS error gets raised and caught here. This gets printed out, else the whole else block gets skipped, so nothing in here happens, but the finally block still executes and this final print statement also gets printed to the console, as well as your program doesn’t quit because you’re handling all of the exceptions that got raised, and so it continues running and you get this final message printed out as well.

02:22 And that’s what’s there to say about the finally keyword. You can add it to your tryexcept block, which makes it consist of this try, except, else, and finally, and you saw all of these keywords implemented in the example.

02:35 And that’s it about the course! In the next and final lesson, you’re going to go again over a quick summary of what you learned about in this course and wrap it up. Then I’ll let you go ahead and write your exceptions into your own code.

Become a Member to join the conversation.