When a failure occurs in the program (such as division by zero, for example) at runtime, an exception is generated. If the exception is not handled, it will be propagated through function calls to the main program module, interrupting execution.
In [1]:
print 1/0
The try instruction allows exception handling in Python. If an exception occur in a block marked by try, it is possible to tread the exception through the instruction except. It is possible to have many except blocks for the same try block.
In [1]:
try:
print 1/0
except ZeroDivisionError:
print 'Error trying to divide by zero.'
If except receives the name of an exception, only that exception will be handled. If no exception name is passed as parameter, all exceptions will be handled.
Example:
In [2]:
import traceback
# Try to get a file name
try:
fn = raw_input('Nome do arquivo: ').strip()
# Numbering lines
for i, s in enumerate(file(fn)):
print i + 1, s,
# If an error happens
except:
# Show it on the screen
trace = traceback.format_exc()
# And save it on a file
print 'An error happened:\n', trace
file('trace.log', 'a').write(trace)
# end the program
raise SystemExit
The module traceback offers functions for dealing with error messages. The function format_exc() returns the output of the last exception formatted in a string.
The handling of exceptions may have an else block, which will be executed when no exception occur and a finally block, which will be executed anyway, exception occurred or not. New types of exceptions may be defined through inheritance of the class Exception.
Since version 2.6, it is available the instruction with, that may substitute the combination try / finally in many situations. It is possible to define an object that will be used during the with block execution. The object will support context management protocol, which means that it will need to have an __enter__()
method, which will be executed on the beginning of the block, and another called __exit__()
, which will be called at the end of the block.
Example:
In [3]:
import random
# Creates a file with 25 random numbers
with file('temp.txt', 'w') as temp:
for y in range(5):
for x in range(5):
# "print >> " records command output on the file
print >> temp, '%.2f' % random.random(),
print >> temp
# Shows file content
with file('temp.txt') as temp:
for i in temp:
print i,
# Out of the blocks, the file will be closed
# The following command generates an exception ValueError: I/O operation on closed file
print >> temp
With file closed at the end of the block, the record attempt generates an exception.
In [1]:
Out[1]: