Parsing exceptions the pythonic way

Daniel Fackrell newsgroups.NOSPAM at dfackrell.mailshell.com
Fri May 30 00:56:48 EDT 2003


"Hal Wine" <hal_wine at yahoo.com> wrote in message
news:3ED615A1.9040500 at yahoo.com...
> It seems this should be easier and cleaner than I've figured out,
> so I guess I don't know python very well yet :(
>
> What's the pythonic way to get error numbers or base messages
> from an exception?  Solution need only work for standard library.
>
> For example, urllib.urlopener catches socket errors and raises
> them as IOError. It also raises non-successful HTTP status values
> as IOError.  Of course, the location of any numeric code or
> message is different in each case.
>
> The options I see are:
> - do some hack in my except clause
> - write my own replication of urllib.urlopener accessing httplib
> directly
> - give up on some of my functionality
>
> None of these seem either "pythonic" or elegant. Nor do they
> generalize to other standard library exceptions.
>
> Thanks,
> --Hal


A friend pointed me at the errno module for handling socket errors.  I
haven't done much socket programming, but the method he was using was to
perform the action (read/write/etc.) on the socket, and catch the error
raised by the operation, and then test the actual error code.  Something
like (untested, nearly guaranteed to not work as-is code below):

try:
    mysocket.receive(buffersize)
except IOError, stuff:
    if stuff == errno.EAGAIN:
        # do whatever you need to do in this case
    else:
        raise

As much as I want to be helpful, I know it is far to late right now to have
much hope.  Sorry.

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list