Skip to content

Instantly share code, notes, and snippets.

@Deiru2k
Created February 12, 2015 11:05
Show Gist options
  • Save Deiru2k/9b6ca8ef07f7a8f48e1c to your computer and use it in GitHub Desktop.
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?
# 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