imaplib & except

Donn Cave donn at oz.net
Thu Oct 5 01:25:14 EDT 2000


Quoth "BSD UNIX" <bsdunix at bellatlantic.net>:
...
| I'm trying to talk to an IMAP server using imaplib.  I can not figure out
| how to catch an exception on the initial request.
|
| I know once a connection is established that "except <instance>.error" will
| work, however if the initial request never succeeds, then the <instance> is
| never created--so how do I catch the exception?

The error is really an attribute of the class.  As such, it's effectively
an attribute of the instance as well, but you can use it directly from
the class.  E.g.,

    try:
        imap = imaplib.IMAP4('testhost')
    except imaplib.IMAP4.error:
        print 'caught'

It's a shame that the error displays as 'imaplib.error', that looks to
me like a kind of defect in the way exceptions are implemented in
general.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list