简体   繁体   中英

Python: How to catch this kind of exception?

I'm making a program for AIX 5.3 in Python 2.6.1 that interfaces with an IMAP server. I'm getting an exception which I don't know how to catch - it doesn't seem to have a name that I can use with "except". The error seems to be some kind of timeout in the connection to the server.

The last part of the stack trace looks like this:

File "/home/chenf/python-2.6.1/lib/python2.6/imaplib.py", line 890, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
abort: command: SEARCH => socket error: EOF

I only want to catch this specific error, so that I can reconnect to the IMAP server when it happens. What's the syntax for catching this kind of exception?

例外是imaplib.IMAP4.abort( Python doc ),因此应该有效

you can try to catch it and find out the type:

import sys,traceback,pprint

try:
    do what you want to do
except:
    type, value, tb = sys.exc_info()
    pprint.pprint(type)
    print("\n" + ''.join(traceback.format_exception(type, value, tb)).strip("\n"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM