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)
In this in-depth tutorial, you'll learn how to build a socket server and client with Python. By the end of this tutorial, you'll understand how to use the main functions and methods in Python's socket module to write your own networked client-server applications.
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)
By Leodanis Pozo Ramos • Updated Aug. 14, 2026