snapcrack
snapcrack

Reputation: 1811

catching all of module's exceptions

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

Answers (1)

Jack
Jack

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

Related Questions