[Fixed]-How to understand how exception handling is working?

1👍

As the code is, there is no real reason for the try because at the end of the try block you always raise an exception, so you always go to the except block. So each time the code runs, it goes through the try, then hits the exception so goes to the except block where it raises an exception no matter which branch of the if/else statement you go to.

This code here does the same thing, it’s just a little cleaner (less duplication) so you can see where the exceptions are being raised

def deleteTable(self, datasetId, tableId):
    GOOG_API_ENDPOINT = 'https://www.googleapis.com/bigquery/v2/projects/{}/datasets/{}/tables/{}'
    url = GOOG_API_ENDPOINT.format(self.PROJECT_ID, datasetId, tableId)

    response, json_content = self.http_auth.request(url, "delete")
    content = json.loads(json_content) # don't reassign same variable name

    if response.status < 300:
        message = tableId + " Was Deleted."
        raise Exception, message  # exception raised here or in else below
    else:
        message = content.get('error', {}).get('message')
        raise Exception, message

Although I wouldn’t raise a generic Exception, I’d use one of the built-ins or define your own

Leave a comment