Open In App

Exception Handling Of Python Requests Module

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python's requests module is a simple way to make HTTP requests. In this article, we’ll use the GET method to fetch data from a server and handle errors using try and except. This will help us understand how to manage situations where the request fails or returns an error."

  • url: Returns the URL of the response
  • raise_for_status(): If an error occurs, this method returns a HTTP Error object
  • request: Returns the request object that requested this response
  • status_code: Returns a number that indicates the status (200 is OK, 404 is Not Found)

Successful Connection Request

The first thing to know is that the response code is 200 if the request is successful.

Python
import requests

r = requests.get("https://www.google.com")

r.status_code

Output:

200

Exception Handling for HTTP Errors

Here, we tried the following URL sequence and then passed this variable to the Python requests module using raised_for_status(). If the try part is successful, we will get the response code 200, if the page that we requested doesn't exist. This is an HTTP error, which was handled by the Request module's exception HTTPError and you probably got the error 404.

Python
import requests

url = "https://www.amazon.com/nothing_here"

try:
    r = requests.get(url, timeout=1)
    r.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print("HTTP Error")
    print(errh.args[0])

print(r)

Output:

HTTP Error
404 Client Error: Not Found for url: https://www.amazon.com/nothing_here
<Response [404]>

General Exception Handling

You could also use a general exception from the Request module. That is requests.exceptions.RequestException.

Python
url = "https://www.google.com"
try:
    r = requests.get(url, timeout=1)
    r.raise_for_status()
except requests.exceptions.RequestException as errex:
    print("Exception request")

Output:

Exception request

Now, you may have noticed that there is an argument 'timeout' passed into the Request module. We could prescribe a time limit for the requested connection to respond. If this has not happened, we could catch that using the exception requests.exceptions.ReadTimeout. To demonstrate this let us find a website that responds successfully.

Python
import requests

url = "https://www.google.com"
try:
    r = requests.get(url, timeout=1)
    r.raise_for_status()
except requests.exceptions.ReadTimeout as errrt:
    print("Time out")

print(r)

Output:

<Response [200]>

If we change timeout = 0.01, the same code would return, because the request could not possibly be that fast.

Time out
<Response [200]>

Exception Handling for Missing Schema

Another common error is that we might not specify HTTPS or HTTP in the URL. For example, We cause use requests.exceptions.MissingSchema to catch this exception.

Python
url = "www.google.com"
try:
    r = requests.get(url, timeout=1)
    r.raise_for_status()
except requests.exceptions.MissingSchema as errmiss:
    print("Missing schema: include http or https")
except requests.exceptions.ReadTimeout as errrt:
    print("Time out")

Output:

Missing scheme: include http or https

Exception Handling for Connection Error

Let us say that there is a site that doesn't exist. Here, the error will occur even when you can't make a connection because of the lack of an internet connection

Python
try:
  r = requests.get(url, timeout = 1, verify = True)
  r.raise_for_status()
except requests.exceptions.HTTPError as errh:
  print("HTTP Error")
  print(errh.args[0])
except requests.exceptions.ReadTimeout as errrt:
  print("Time out")
except requests.exceptions.ConnectionError as conerr:
  print("Connection error")

Output:

Connection error

Putting Everything Together

Here, We put together everything we tried so far the idea is that the exceptions are handled according to the specificity. 

For example, url =  "https://www.gle.com",  When this code is run for this URL will produce an Exception request. Whereas, In the absence of connection requests.exceptions.ConnectionError will print the Connection Error, and when the connection is not made the general exception is handled by requests.exceptions.RequestException.

Python
url = "https://www.gle.com"

try:
    r = requests.get(url, timeout=1, verify=True)
    r.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print("HTTP Error")
    print(errh.args[0])
except requests.exceptions.ReadTimeout as errrt:
    print("Time out")
except requests.exceptions.ConnectionError as conerr:
    print("Connection error")
except requests.exceptions.RequestException as errex:
    print("Exception request")

Output:

Note: The output may change according to requests.

 Time out

Related articles:


Next Article
Article Tags :
Practice Tags :

Similar Reads