how do you add azure python sdk exceptions to tryexcept statements?

The exception will be caught in order of “except” clauses, but beware of the subclass tree, since a except will catch all subclasses as well. For instance, this one leads to unreachable code.

     # do something
except BaseException:
     # do something with
except DerivedException:
     # assuming DerivedException is an extension of BaseException, you can't reach that code

So put them in most specific first.

In your Azure situation, this brings to something like:

from azure.core.exceptions import (

    # do KV stuff
except ClientAuthenticationError:
    # Can occur if either tenant_id, client_id or client_secret is incorrect
    logger.critical("Azure SDK was not able to connect to Key Vault", e)
except HttpResponseError:
    # One reason is when Key Vault Name is incorrect
    logger.critical("Possible wrong Vault name given", e)
except ServiceRequestError:
    # Network error, I will let it raise to higher level
except ResourceNotFoundError:
    # Let's assume it's not big deal here, just let it go
except AzureError as e:
    # Will catch everything that is from Azure SDK, but not the two previous
    logger.critical("Azure SDK was not able to deal with my query", e)
except Exception as e:
    # Anything else that is not Azure related (network, stdlib, etc.)
    logger.critical("Unknown error I can't blame Azure for", e)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top