What is the point of the finally
clause? Why not put any statements you want executed after the try
block (regardless of whether or not an exception has been raised) after the entire try ... except
clause?
Hint: See what happens if you modify Example E4.5 to put the statements in the finally
clause after the try
block.
The point of finally
in Example E4.5 is that statements in this block get executed before the function returns. If the line
print(' Done with file {}'.format(filename))
were moved to after the try
block, it would not be executed if a IOError
exception is raised (because the function would have returned to its caller before this print
function is encountered.)