f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
- except IOError, (errno, strerror):
+ except IOError as (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
>>> try:
... raise Exception('spam', 'eggs')
- ... except Exception, inst:
+ ... except Exception as inst:
... print type(inst) # the exception instance
... print inst.args # arguments stored in .args
... print inst # __str__ allows args to printed directly
...
>>> try:
... this_fails()
- ... except ZeroDivisionError, detail:
+ ... except ZeroDivisionError as detail:
... print 'Handling run-time error:', detail
...
Handling run-time error: integer division or modulo by zero
...
>>> try:
... raise MyError(2*2)
- ... except MyError, e:
+ ... except MyError as e:
... print 'My exception occurred, value:', e.value
...
My exception occurred, value: 4