[Tutor] Error handling

Michael Lewis mjolewis at gmail.com
Sun Mar 25 07:12:54 CEST 2012


On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers <colton.myers at gmail.com>wrote:

>  I am having a bit of trouble understanding what is going on below. What
> does the "e" in "except OSError, e:" do?
> Any other help you can provide regarding errno would be extremely
> appreciated. I've done help() and dir() on it, but I am not really
> understanding what's going on with "e.errno != errno.EEXIST:"
>
> Basically, that `except` block is catching all exceptions of type OSError,
> and storing the exception in variable `e`.  This variable does not have to
> be called `e`, but that's the most commonly-used variable name.
>
> Once you have the exception stored (in this case in the variable `e`), you
> can then see what type of exception, using the `errno` property of the
> exception.  You can read about the different types here:
>
> http://docs.python.org/library/errno.html
>
>  import os, errnotry:
>     os.makedirs('a/b/c')except OSError, e:
>     if e.errno != errno.EEXIST:
>         raise
>
> In this particular section, it's catching any OSError, and then if it
> turns out that the error was "File Exists", it is raising that exception
> again, to be either caught by an encapsulating try block, or which will
> bring the program to a halt with an exception shown by the interpreter.
>
> Is that the behavior you are going for?  Any more confusion?
>

Why wouldn't it be errno.e instead of e.errno?

>
> --
> Colton Myers
>
>


-- 
Michael J. Lewis

mjolewis at gmail.com
415.815.7257
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120324/a169b2da/attachment.html>


More information about the Tutor mailing list