How to Raise an Error for a Failed Request in Python with raise_for_status
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
try:
response.raise_for_status()
except requests.HTTPError as error:
... # what to do with the failed request
| Part | What 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/404always answers404 Not Found.
Step 1: Ask for a Missing Page
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)

Step 2: Run It
uv run python raise_for_status.py
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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting requests.get to fail on a 404 by itself | It returns the response normally; nothing raises until you call raise_for_status() | Call it right after the request |
Catching HTTPError around requests.get alone | The error comes from raise_for_status(), so it must be inside the try | Put the call inside the try |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The program stops with requests.exceptions.HTTPError | Nothing catches the error | Wrap the call in try and except requests.HTTPError |
You need the status code inside the except | The message is text | error.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
- requests quickstart: Response Status Codes —
raise_for_statusand status codes. - requests API: Response.raise_for_status — the method's reference.
