Reputation: 1811
I have a script and keep getting different exceptions thrown. Right now, my code is written like:
from requests.exceptions import InvalidURL, TooManyRedirects, InvalidSchema
try:
#do thing
except (InvalidURL, TooManyRedirects, InvalidSchema):
pass
But requests has a lot of exceptions and typing them all out is laborious. Is there a way to do something along the lines of:
import requests.exceptions
try:
#do thing
except e if e in requests.exceptions:
pass
Upvotes: 0
Views: 288
Reputation: 2625
All the exceptions in the module requests.exceptions
derive from the base class RequestException
.
If you really want to catch any and all exceptions you can do so:
from requests.exceptions import RequestException
try:
# do thing
except RequestException:
# handle exception
Upvotes: 2