Skip to content

Commit

Permalink
finally statement in exception chapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
kushaldas committed Jul 12, 2013
1 parent b0e744b commit f61a3ff
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,25 @@ We can catch these exceptions like any other normal exceptions.
...
ValueError in our code.

Using finally for cleanup
==========================

If we want to have some statements which must be executed under all circumstances,
we can use `finally` clause, it will be always executed before finishing `try`
statements.
::

>>> try:
... fobj = open("hello.txt", "w")
... res = 12 / 0
... except ZeroDivisionError:
... print "We have an error in divition"
... finally:
... fobj.close()
... print "Closing the file object."
...
We have an error in divition
Closing the file object.

In this example we are making sure that the file object we open, must get closed
in the `finally` clause.

0 comments on commit f61a3ff

Please sign in to comment.