Created
February 12, 2015 11:05
-
-
Save Deiru2k/9b6ca8ef07f7a8f48e1c to your computer and use it in GitHub Desktop.
How I deal with exceptions and how I'm /supposed/ to deal with them. Should I continue, or is it a bad practice?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Case One: How /it should be/ done: | |
class CloudStorageRaises(object): | |
def upload_picture(file): | |
try: | |
url = self.storageApi.upload_from_file(file).generate_url() | |
return url | |
except Exception as e: | |
logging.log(e.message) | |
raise e | |
class MyAwesomeCodeCatches(object): | |
storage = CloudStorageRaises() | |
def someCode(): | |
try: | |
url = self.storage.upload_picture(file) | |
except: | |
pass # actually do something, but that's not important | |
# Case Two: How /I do it/: | |
class CloudStorageReturns(object): | |
def upload_picture(file): | |
try: | |
url = self.storageApi.upload_from_file(file).generate_url() | |
return url | |
except Exception as e: | |
logging.log(e.message) | |
return None | |
class MyAwesomeCodeChecks(object): | |
storage = CloudStorageReturns() | |
def someCode(): | |
url = self.storage.upload_picture(file) | |
if not url: pass # actually do something, but that's not important |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment