Skip to main content

How to Raise an Error for a Failed Request in Python with raise_for_status

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

requests doesn't treat a 404 or 500 answer as an error: it returns the response and carries on. When a failed answer should stop your code, response.raise_for_status() turns it into a requests.HTTPError that says what went wrong. This guide asks for a page that doesn't exist and catches the error, with real output from Python 3.15.2 and requests 2.34.2.

Syntax​

Syntax
try:
response.raise_for_status()
except requests.HTTPError as error:
... # what to do with the failed request
PartWhat it means
response.raise_for_status()Raises requests.HTTPError for a 4xx or 5xx status code, and does nothing otherwise
except requests.HTTPError as error:Catches it; error holds the message

The requests quickstart: "If we made a bad request (a 4XX client error or 5XX server error response), we can raise it with Response.raise_for_status()".

Prerequisites​

  • Python 3 with the requests package in your project, for example with uv add requests.
  • An internet connection: httpbin.org's /status/404 always answers 404 Not Found.

Step 1: Ask for a Missing Page​

raise_for_status.py
import requests

response = requests.get("https://httpbin.org/status/404", timeout=10)

try:
response.raise_for_status()
except requests.HTTPError as error:
print("The request failed:", error)

The raise_for_status.py script in a macOS-style code window: requests.get to httpbin.org/status/404, then response.raise_for_status() inside try, and except requests.HTTPError as error printing The request failed and the error.

Step 2: Run It​

Terminal
uv run python raise_for_status.py
Output
The request failed: 404 Client Error: Not Found for url: https://httpbin.org/status/404

The script's output in a terminal: The request failed: 404 Client Error: Not Found for url: https://httpbin.org/status/404.

The error names the code, the reason and the address. A 500 gave 500 Server Error: Internal Server Error for url: https://httpbin.org/status/500 the same way.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting requests.get to fail on a 404 by itselfIt returns the response normally; nothing raises until you call raise_for_status()Call it right after the request
Catching HTTPError around requests.get aloneThe error comes from raise_for_status(), so it must be inside the tryPut the call inside the try

Troubleshooting​

SymptomLikely causeFix
The program stops with requests.exceptions.HTTPErrorNothing catches the errorWrap the call in try and except requests.HTTPError
You need the status code inside the exceptThe message is texterror.response.status_code gave 500 for the 500 answer

FAQ​

What does it do for a successful answer?​

Nothing: for a 200, response.raise_for_status() returned None, and the code carried on.

Does it catch connection problems too?​

No. It only looks at a response that arrived. A server that can't be reached raises requests.ConnectionError earlier, in requests.get itself.

Conclusion​

raise_for_status() turned httpbin.org's 404 into 404 Client Error: Not Found for url: https://httpbin.org/status/404, and except requests.HTTPError printed it instead of letting the failed answer slip through.

Additional Resources​