ConnectionError
In Python, ConnectionError is a built-in exception and the base class for connection-related issues. Python doesn’t raise it directly. Instead, it raises one of its four subclasses: BrokenPipeError, ConnectionAbortedError, ConnectionRefusedError, and ConnectionResetError.
This exception is a direct subclass of OSError, and it covers the OS-level conditions that Python maps to its four subclasses: refused connections (ECONNREFUSED), reset connections (ECONNRESET), aborted connections (ECONNABORTED), and broken pipes (EPIPE, ESHUTDOWN).
You can use ConnectionError to handle network-related errors in your code. This can help you implement robust error-handling strategies, such as retrying failed network requests or logging issues for further investigation.
ConnectionError Occurs When
- A peer refuses a connection attempt (
ECONNREFUSED) - A peer resets an established connection (
ECONNRESET) - A peer aborts a connection attempt (
ECONNABORTED) - Code writes to a pipe or socket whose other end is closed or shut down for writing (
EPIPE,ESHUTDOWN)
ConnectionError Can Be Used When
- Handling errors when trying to connect to a remote server or service
- Managing exceptions during HTTP requests made with the standard library, such as
socketorhttp.client, but not calls made with therequestslibrary, which defines its own unrelatedrequests.exceptions.ConnectionErrorthatexcept ConnectionErrorwon’t catch - Capturing connection failures in client-server applications
Related Resources
Tutorial
Socket Programming in Python (Guide)
Build a Python socket server and client from scratch, then extend it to handle multiple connections at once without blocking.
For additional information on related topics, take a look at the following resources:
- Python Exceptions: An Introduction (Tutorial)
- Python's Built-in Exceptions: A Walkthrough With Examples (Tutorial)
- Python's raise: Effectively Raising Exceptions in Your Code (Tutorial)
- Programming Sockets in Python (Course)
- Socket Programming in Python (Quiz)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)
- Working With Python's Built-in Exceptions (Course)
- Python's Built-in Exceptions: A Walkthrough With Examples (Quiz)
- Using raise for Effective Exceptions (Course)
- Python's raise: Effectively Raising Exceptions in Your Code (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Sept. 21, 2026