- [Instructor] In this video,
I'd like to talk about the finally clause
of a try statement.
Now the finally clause, is guaranteed to execute
if the normal flow of control of your Python code
enters a try statements suite.
So if I go into the try statement and start executing,
and the try statement has a finally clause at the end,
that finally clause is guaranteed to execute
with the only exception to that being,
if you manually terminate the code
by calling system.exit for example.
Now, if you're coming into Python
from a language like C# or Java
that has a finally clause on the try statement,
you're already familiar with the basic concept.
If you're not, the idea is
that in a try statement, you might acquire a resource
and then you want to use that resource
and maybe an exception could occur
if you're working with that resource
and sometimes it will and sometimes it won't,
but you want to absolutely guarantee
that you give that resource back to the system
before the try system terminates,
so resource deallocation code
would typically be placed in a finally block or suite
as we call it in Python.
Now in Python, those types of resources
are typically acquired in with statements
like we demonstrated with files earlier in this lesson,
and with statements automatically
give those resources back to the system.
So there's less need for a finally block,
unless you're dealing with some sort of resource,
that does not have that automated support
for working in a with statement.
So it's there in case you need it,
but in the vast majority of cases
for those types of resources,
we'd use with statements in Python.
But let's go ahead and demonstrate the basic finally block
and we'll use a mechanical example for this purpose.
Here I've pasted in a try statement.
In the try suite, we have just a print statement
that will print out try suite with no exceptions raised
and you can see I am not doing something
that would cause an exception here.
I'm not, for example, converting the string hello
to an integer which cannot happen
and therefore would cause an exception.
I'm not using the raise function that's built into Python
to manually cause an exception either,
so the try block will execute successfully here,
which means the except clause will be ignored.
So this will not execute, will not get printed.
The else cause will execute
because as long as no exceptions occur in the try block,
the else clause will execute.
So else executes because no exceptions in the try suite
that will be printed, and because we executed the try suite,
the finally block or the finally suite will also execute
printing finally always executes.
So we should see the first print statements output,
the third print statements output
and the finally print statements output as well
and if you check these, you'll see that
that is indeed the case in this example.
Now let's once again, take a look at a try statement
this time for a try suite
in which an exception actually does occur.
So here I've pasted in another try statement.
In this try statement, we will print try statement
that raises an exception,
and then we will attempt to call the built in function int
to convert the string hello to an integer.
Now of course the string hello
is not a valid integer representation as a string
and therefore this will cause a value error.
Now the try suite will terminate at this point,
which is known as the raise point of the exception
that occurs.
Any statements that follow the highlighted statement here
will not execute when that exception occurs
and flow of control will continue
with the except clause for the value error.
So we will print out try suite that raises an exception,
then we will jump down here
and print out a value error occurred
because an exception occurred,
the else suite will be ignored,
but the finally suite always executes.
So in this case, we expect
to see the first print statement execute,
the except clauses print statement execute,
and the finally clauses print statement execute
and as you can see here, those are the three prints
that we did indeed get.
Now going back to what I said earlier
about this issue that in Python,
most resources that need to be deallocated
when you're done using them will work with a with statement.
So it's not uncommon if you're trying
to deal with exceptions that might occur
from those resources like working with a file on disk
to wrap a with statement in a try statement.
So let me first demonstrate an exception that occurs
if we try to open a file that does not exist.
So first thing I'm gonna do here is just clear my screen
so that I can get back to the top here
and let's say we want to open a file that does not exist
in my system.
I misspelled grades here on purpose.
In that case, I get an exception.
A file not found error indicating that a problem occurred.
Now normally I would not execute this function call
in a snippet by itself,
instead I would probably have that in a with statement
that says go ahead and open that,
assign the open file to a particular variable name
and then process the data in that file in some way.
So let's take a look at what you might do
to combine a with statement with a try statement.
So here we have a try statement
that's going to attempt
to open the gradez.txt file for reading.
This file does not exist.
If it does get opened, it would be assigned
to the variable accounts
and then we would do some processing
of that file of information.
Now in this case, the file does not exist,
so because of the fact that we've put the with statement
in the try suite, we can look for
and deal with that exception.
So if the exception occurs, the except clause
for the file not found error will match the exception
that occurred when we tried to call open
and we will print
the file name you specified does not exist.
So let me go ahead and execute that snippet,
and indeed our print statement in the except clause executes
rather than us displaying
this ugly file not found error trace back
that we saw back in snippet four.
So of course if we're creating larger applications
and scripts that other people are going to execute,
we're probably going to want
to use some more exception handling
to make it a little bit nicer
for the user interacting with that application or script.