All of the exceptions in the standard library that are expected to be "generally usable" are built-ins, and are documented in the Built-in Exceptions part of the library reference.
In 3.3, that includes this one:
exception ConnectionError
A base class for connection-related issues.
Subclasses are BrokenPipeError
, ConnectionAbortedError
, ConnectionRefusedError
and ConnectionResetError
.
But this is a built-in. So this should work:
except ConnectionError:
In 3.0-3.2, there is no such exception as ConnectionError
. Nothing in the stdlib raises anything of that name. So there's no point in trying to handle it. (See PEP 3151 for an explanation of how OSError
and IOError
were reorganized between 3.2 and 3.3.)
The 3.2 equivalent of ConnectionError
is OSError
with certain errno
values. So, what you want is something like:
except OSError as e:
if e.errno not in (EPIPE, ESHUTDOWN, ECONNABORTED, ECONNREFUSED, ECONNRESET):
raise
# whatever you wanted to do for ConnectionError.
Meanwhile, in the future, when you don't know what kind of exception you need to handle, it's pretty easy to test. First, write some test code that handles any exception by logging the qualified name of the exception type. Then take the type out of the log and use that in your real code.
try:
code_that_raises()
except Exception as e:
print(type(e), type(e).__qualname__, whatever_else_looks_useful(e))