Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkutaj/a078d3406ca90997b22db9e3c2b42ecf to your computer and use it in GitHub Desktop.
Save pkutaj/a078d3406ca90997b22db9e3c2b42ecf to your computer and use it in GitHub Desktop.
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