Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Request Module Exception Handling Template
NR CODE COMMENT
#1 response = requests.get(url,...) within a try block, create variable with a response object
#2 response.raise_for_status() raises exception 4xx & 5xx HTTP Statuses/errors
#3 except requests.exceptions.RequestException as e: exception base class for all other exceptions
#4 print(e, file=sys.stderr) print exception object into stderr stream
import requests
import sys
url = "http://example.com"
try:
response = requests.get(url,...) #1
response.raise_for_status() #2
except requests.exceptions.RequestException as e: #3
print(e, file=sys.stderr) #4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment