[Tutor] Error handling
Colton Myers
colton.myers at gmail.com
Sat Mar 24 23:51:06 CET 2012
> 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, errno try: 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?
--
Colton Myers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120324/9bc9d3da/attachment.html>
More information about the Tutor
mailing list