Python beginner with an exception handling question.
August 16, 2007 12:55 PM   Subscribe

[PythonFilter] How does one handle specific errno of a built-in exception?

Both 'invalid path' and 'file does not exist' fall under IOError. How can I handle each case separately without resorting to custom exceptions?
posted by nilihm to Computers & Internet (10 answers total) 1 user marked this as a favorite
 
(First: Is there really a difference between the two?)

Strip off the directory and test if it exists first. Then, see if the file within it exists (by opening it).
posted by cmiller at 1:24 PM on August 16, 2007


What do you mean by "separately without custom exceptions"? Won't you have to write code for both?
posted by unixrat at 1:41 PM on August 16, 2007


Oh, and speaking overall, exceptions are nice for signaling something wrong from far away, but it's probably a bad idea to use them for program flow. Use them for truly exceptional events, not in place of an "if".
posted by cmiller at 1:42 PM on August 16, 2007


Response by poster: I mean that I'd like to handle an IOError according to its errno. Sometimes it's 2, other times 9, etc. I do not want to raise a custom defined exception.
posted by nilihm at 1:49 PM on August 16, 2007


Just use e.errno:

except IOError, e:
__if e.errno == 2:
____print 'The errno was 2!'
__elif e.errno == 9:
____print 'The errno was 9!'
__(...)


e.strerror is the human-readable error description.
posted by pocams at 2:21 PM on August 16, 2007


Oh, and speaking overall, exceptions are nice for signaling something wrong from far away, but it's probably a bad idea to use them for program flow. Use them for truly exceptional events, not in place of an "if".

I'm just learning me some python too, actually.. and a lot of tutorials seem to be encouraging use of try / except, so..
posted by Chuckles at 2:54 PM on August 16, 2007


Response by poster: Agreed, Chuckles, that's why I tried writing in that style.
posted by nilihm at 3:14 PM on August 16, 2007


Best answer: pocams has it.

If you want to be portable, instead of comparing against specific errno values (which can vary between platforms), you can import the errno module and compare against the names of the errors you're trying to deal with. For example, instead of e.errno == 2, you would say e.errno == errno.ENOENT.

On a Unix system, you should be able to check the errno man page to see what all the names are and what they mean. You can also figure out what number corresponds to what error name on your platform by checking errno.errorcode. On my system, errno.errorcode[2] == 'ENOENT'.
posted by brett at 3:52 PM on August 16, 2007


Best answer: Please don't compare against hard-coded numeric values or e.strerror. This will not be portable. Instead, use the errno module as brett suggests.

cmiller, the prevailing Python idiom is that it's Easier to Ask Forgiveness than Permission (EAFP). This means trying operations and handling exceptions. The language was designed with the expectation that you would program this way, and doing so has several benefits.

The most tangible of these benefits are that it avoids race conditions. It's not good enough to check that a file exists before you start reading it, because some other program (or even another thread in your own program) might have created or deleted the file between your check and when you start reading. If you want to also cover this case, you'll have to deal with the resulting exception. And if you're going to deal with the exception, why bother to put your code to deal with the file not existing an extra time before reading?
posted by grouse at 3:57 PM on August 16, 2007


Response by poster: Thanks a lot. You've been very helpful.
posted by nilihm at 4:31 PM on August 17, 2007


« Older mounting paper to wood   |   What should I keep firesafe on a USB stick? Newer »
This thread is closed to new comments.