python exception message capturing

You have to define which type of exception you want to catch. So write except Exception, e: instead of except, e: for a general exception (that will be logged anyway). Other possibility is to write your whole try/except code this way: try: with open(filepath,’rb’) as f: con.storbinary(‘STOR ‘+ filepath, f) logger.info(‘File successfully uploaded to ‘+ … Read more

Safe method to get value of nested dictionary

You could use get twice: example_dict.get(‘key1’, {}).get(‘key2’) This will return None if either key1 or key2 does not exist. Note that this could still raise an AttributeError if example_dict[‘key1’] exists but is not a dict (or a dict-like object with a get method). The try..except code you posted would raise a TypeError instead if example_dict[‘key1’] … Read more

What is wrong with using a bare ‘except’? [duplicate]

Bare except will catch exceptions you almost certainly don’t want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit If you don’t have a specific exception you’re expecting, at least except Exception, which is the base type for all “Regular” exceptions. That being said: you use except blocks to recover from … Read more