5

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?

2 Answers 2

10

The exception is imaplib.IMAP4.abort (Python doc) so catching that should work

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I understand now. I thought self.abort() was a function (which confused me, how can you use a function as an exception?), but now I realise it's an exception! Thanks!
3

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")) 

4 Comments

-1, OP knows how to handle the exceptions, his problem was to know how to catch self.abort, and keep your answer formated, pasting code needs formating with code TAGs code
I fixed your post. it's except:, not Exception. Other than that the code is as you wrote it and I just fixed the formatting. If you click 'edit', then you will be able to see how it works. Just start everything you want formatted four spaces to the right.
@shahjapan, that is ok that he knows hoe to catch exception, but if he does not know how to check its type, this is a way. That's why I sent the code.
This is useful. The standard except message does not show the full class name of the exception, but the print(type) in this example does show the type. In my case, the exception was displayed as "error" but the actual exception was "socket.error". Catching "error" didn't do anything!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.