Lazo Lab Sign up

App 4: Weather App with a Real API / Build the weather app

Lesson 7 of 10

Handling network errors

The internet fails sometimes. Catch requests exceptions so the app explains the problem instead of crashing.

Key points

  • requests.RequestException catches network problems
  • Tell the user what to try
  • Never hide errors silently
try:
    now = get_weather(lat, lon)
except requests.RequestException:
    print("Couldn't reach the weather service. Check your internet and try again.")

Your turn

Turn off your Wi-Fi and run the app. Make sure it shows your friendly message.

Watch a video on thisOpens YouTube search results for “Handling network errors” in a new tab

Quiz · +10 XP

Which exception covers most requests problems?

Log in to save progress and earn XP.