Python Glossary Русский

Python 3.3+Exceptionsexception

ConnectionError

built-in exceptions

Base class for connection-related errors (a subclass of OSError).

Syntax

raise ConnectionError

Catch this one when the specific cause does not matter: it covers BrokenPipeError, ConnectionAbortedError, ConnectionRefusedError and ConnectionResetError in a single clause. Not every networking failure is underneath it, though — a timeout is TimeoutError and an unresolvable host name is socket.gaierror, both descending from OSError while bypassing ConnectionError. One more trap for requests users: requests.exceptions.ConnectionError is a different class entirely and the builtin except ConnectionError will not catch it.

Examples

try:
    raise ConnectionError
except ConnectionError as e:
    print(type(e).__name__)   # → ConnectionError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy