Re: Granularity of OSError



On Sep 18, 11:54 am, kj <no.em...@xxxxxxxxxxx> wrote:
I've often come across the idea that good Python style deals with
potential errors using an EAFP ("easier to ask forgiveness than
permission") strategy rather than a LBYL ("look before you leap")
strategy.

For example, LBYL would look like this:

if os.path.isfile(some_file):
    os.unlink(some_file)

In contrast, EAFP would look like this:

try:
    os.unlink(some_file)
except OSError:
    pass

But, as written, the EAFP code above is not satisfactory, because
there can be OSError's triggered for reasons other than the
non-existence of the regular file some_file.

What one needs is a finer granularity of exception, mapping to
exactly the error that one is guarding against.

Is there a standard approach to refining the granularity of exceptions
such as OSError?  The only approach I can think of is to somehow
parse the error string (assuming is available) to determine whether
the exception is indeed of the specific kind we're trying to catch.
But this strikes me as grossly inelegant.  Is there a better way?

(BTW, the problem is generic, because client code has no control
over the specificity of the exceptions raised by a library module.
If these exceptions turn out to be too broad, client code has to
somehow deal with this reality, at least in the short term.)

TIA!

kynn

You can access the exception object which gives you greater detail.

try:
os.unlink(some_file)
except OSError, e:
print e.errno
print e.strerror

if e.errno == 2:
pass
else:
raise


If it's the error you are looking for, handle it, or else raise.

~Sean
.



Relevant Pages

  • Re: What c.l.pys opinions about Soft Exception?
    ...     return len ... handle a Soft Exception, it exists there if you need to handle the ... obfuscate the code since we have to separate distance calculation from ... it terminates the distance calculation ...
    (comp.lang.python)
  • Re: Castrated traceback in sys.exc_info()
    ... Traceback: ...     return d ... the retrieval of the FULL traceback at exception raising point, ... stack trace of an exception. ...
    (comp.lang.python)
  • Re: Version 1.1 on Window Mobile 6.1
    ... Hey Robert...I googled the exception eventtype you listed - ... uninstalling the install of the 1.1 runtime and letting the native 2.0 ... Code    : 0xC0000005 ... CPU Architec: 5 ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: CMutex /CEvent (multiple threads)
    ... anything else provokes an exception. ...     protected: ... and we throw a CSyncTimeout exception on timeout!) ... and in the Lock() code it would look something like ...
    (microsoft.public.vc.mfc)
  • Re: Avoiding Default Parameter Checking in C# 4.0
    ... Occasionally an exception needs thrown in the middle of the private ...    void MethodA ... null and that it is not IList, why pay for the runtime overhead? ... calling a class's public methods from within the class. ...
    (microsoft.public.dotnet.languages.csharp)