← VCE Software Development U4O1

Handling API errors

Check a response's status code (200 OK, 404 not found) and wrap requests.get() in try/except so a failed API call shows a friendly message, not a crash.

Understanding Handling API errors in VCE Software Development U4O1

What it does: This branch tests for the 404 not found code, so the program can show a message instead of using data that does not exist. The call that might fail goes inside try, so if it raises an exception the except block runs and the program keeps going. 200 means OK: the server found the data and sent it back. 404 means the resource was not found. Use == to compare. A single = is assignment and causes a syntax error in an if statement. A friendly message is clear and tells the user what to do. Raw error text confuses users and is poor feedback.

Common mistakes: A single = assigns a value and is a syntax error in an if condition. Use == to compare the status code with 200. The except line must end with a colon. It should be except requests.Timeout:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 115–124) and word search (Modules 115–171).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which status code means an API request worked?
404200
Matches
if response.status_code = 200:
↓
A single = assigns a value, so it is a syntax error. Use == to compare the status code with 200.
Applying
Meanings
Term
response.status_code
Meaning
Check a response's status code (200 OK, 404 not found) and wrap requests.get() in try/except so a failed API call shows a friendly message, not a crash.
Practice
import requests
Testing
Gaps
if response. == 200:
Check Answer
Spots
3r = requests.get(url)
4if r.status_code = 200:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Polar Bear' badge — Data Heavyweight, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which status code means an API request worked?
A. 200 ✓
B. 404
200 means OK: the server found the data and sent it back. 404 means the resource was not found.
2. Which line checks whether the request succeeded?
A. if response.status_code = 200:
B. if response.status_code == 200: ✓
Use == to compare. A single = is assignment and causes a syntax error in an if statement.
3. What does a status code of 404 tell your program?
A. The requested resource was not found ✓
B. The request worked and the data is ready
404 means not found, for example a wrong URL or ID. Check the code before reading the data.
4. Which code stops the program from crashing if the internet connection drops?
A. response = requests.get(url) print("Could not connect")
B. try: response = requests.get(url) except requests.exceptions.RequestException: print("Could not connect") ✓
A failed connection raises an exception. Only try/except can catch it so the program keeps running.
5. Which message is better for a user when the API call fails?
A. Sorry, we could not load the data. Please try again. ✓
B. requests.exceptions.ConnectionError: HTTPSConnectionPool
A friendly message is clear and tells the user what to do. Raw error text confuses users and is poor feedback.

Match the Pairs

response.status_code == 200 → Checks whether the API call succeeded, because status code 200 means OK and the data came back.
except requests.exceptions.RequestException: → Catches a failed API call, such as no internet or a timeout, so the program can show a friendly message instead of crashing.
A 404 status code means the resource was not found, so the program should tell the user rather than read the data. → elif response.status_code == 404:
Wrap requests.get() in a try block so that a connection failure can be handled by an except block. → try: response = requests.get(url)
if response.status_code = 200: → A single = assigns a value, so it is a syntax error. Use == to compare the status code with 200.
response = requests.get(url) print(response.json()) → The call is not in try/except and the status is not checked, so a failed call crashes the program or gives bad data.

Key Term

response.status_code
Check a response's status code (200 OK, 404 not found) and wrap requests.get() in try/except so a failed API call shows a friendly message, not a crash.

Fill in the Blanks

1. if response.status_code == 200:
Hint: Type the response attribute that holds the number showing whether the request worked.
2. try: response = requests.get(url) except requests.exceptions.RequestException:
Hint: Type the keyword that starts the block of code which might fail.
3. response = requests.get(url) if response.status_code == 404: print("Not found")
Hint: Type the variable name that holds the result of the API call.
4. try: r = requests.get(url, timeout=5) except requests.exceptions.RequestException:
Hint: Type the base exception class that catches any error raised by the requests library.
5. except requests.exceptions.RequestException: print("Could not connect.")
Hint: Type the function that shows the user a friendly message instead of crashing.

Spot the Error

Question 1
A import requests
B url = 'https://x.com/api'
C r = requests.get(url)
D if r.status_code = 200:
E print('Success')
Line D — A single = assigns a value and is a syntax error in an if condition. Use == to compare the status code with 200.
Question 2
A try:
B r = requests.get(url)
C except requests.Timeout
D print('Server too slow')
Line C — The except line must end with a colon. It should be except requests.Timeout:
Question 3
A r = requests.get(url)
B if r.status_code == '404':
C print('Not found')
D else:
E print(r.json())
Line B — status_code is an integer, so comparing it with the string '404' is never true. It should be == 404 with no quotes.
Question 4
A import request
B try:
C r = requests.get(url)
D except requests.Timeout:
E print('Try again later')
Line A — The library is called requests, with an s. Importing request fails with a ModuleNotFoundError.
Question 5
A r = requests.get(url)
B if r.status_code == 200:
C data = r.json
D else:
E print('Failed')
Line C — Without brackets, data holds the json method instead of the response data. It should be r.json() to call the method.

Student with a class code? Join your class →