4

Currently I have the problem that I get a NameError for the following code:

try:
    # some network programming raising an exception
except ConnectionError:
    # some error handling

This does not work, because you have to import ConnectionError from some module, which is not mentioned in the documentation (maybe I'm just blind?). All I found is this, but it refers to another request library.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
mafrasi2
  • 312
  • 1
  • 3
  • 14
  • It looks like you're using requests? It's docs are at http://docs.python-requests.org/en/latest/ – ninMonkey Oct 05 '13 at 01:07
  • I am using http.client.HTTPConnection – mafrasi2 Oct 05 '13 at 01:11
  • What Python version are you using? From `http.client.HTTPConnection` it's obviously 3.x. But if you're getting a `NameError` on `ConnectionError`, it's not 3.2, 3.3, or 3.4. – abarnert Oct 05 '13 at 01:27
  • 1
    Sorry, for some reason I though the OSError reorg was in 3.2; it's actually 3.3. Let me edit my answer. But the short version is: You're _not_ getting a `ConnectionError`. Why do you think that's what you want to handle? – abarnert Oct 05 '13 at 01:34
  • @mafrasi2: In other words, your problem isn't that you can't find `ConnectionError` in the 3.2 docs, but that you read some 3.3 docs, or some sample code written for 3.3, or something else that doesn't apply to 3.2, and it sent you on a wild goose chase. – abarnert Oct 05 '13 at 01:41
  • jep, I wrote that code using python 3.3 on another machine and just didn't notice I have 3.2 on my notebook. – mafrasi2 Oct 05 '13 at 01:44
  • @mafrasi2: Well, I made the same mistake as you, as evidenced by my answer before editing it, so don't feel too bad. :) – abarnert Oct 05 '13 at 01:50

2 Answers2

5

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))
Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I am running python 3.2.3, but this does not work either. I cannot import the exceptions module and without import I get another NameError for exceptions :( – mafrasi2 Oct 05 '13 at 01:33
  • 1
    @mafrasi2: OK, updated; 3.2 is the same as 3.1, not the same as 3.3. There is no such error as `ConnectionError`. – abarnert Oct 05 '13 at 01:36
  • Wow, I can't believe, this is true, but you are so right. I never got a ConnectionError, it was a socket.error – mafrasi2 Oct 05 '13 at 01:41
0

I had a similar problem. I'm using python 3.11, the code block you have worked without an import because ConnectionError is in a python built-in. However, the exception was not being caught.

It turns out requests 2.31.0 is using its own ConnectionError (!). Once my except clause used requests.exceptions.ConnectionError, it worked for me.

dfrankow
  • 20,191
  • 41
  • 152
  • 214