Python – identifying specific exception types

I was trying to work with DocuSign and was getting HTTPError 401 Unauthorized in response to the API calls that I wanted to handle specifically. I found some notes online to specify the Exception Type along with the except clause, e.g. except urllib2.HTTPError as ex. But this certainly didn’t work in my case. So I found a different way to handle such exceptions:

try:
    #-- Make API call to get HTTPError in return
except Exception as ex:
    if isinstance(ex, urllib2.HTTPError) and ex.code == 401: #-- Unauthorized
        #-- Now do you handling Here
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s