[Tutor] Error handling

Russel Winder russel at winder.org.uk
Sun Mar 25 09:22:50 CEST 2012


Michael,

On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote:
[...]

It is perhaps worth noting that in Python 3, the syntax has changed:

> import os, errno
> try:
> 
>     os.makedirs('a/b/c')
> except OSError, e:

except OSError as e :

> 
>     if e.errno != errno.EEXIST:
> 
>         raise

This "as" syntax works in 2.6 and 2.7 so is probably the syntax to use
unless you have to use very old versions of Python.  I think the "as"
syntax makes it clearer that e is a variable referring to the instance
of OSError that causes the except clause to execute if it does.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/tutor/attachments/20120325/f91ba36f/attachment.pgp>


More information about the Tutor mailing list