Skip to content

Instantly share code, notes, and snippets.

@giwa
Created March 6, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giwa/fc75d4c14970c0ec2e28 to your computer and use it in GitHub Desktop.
Save giwa/fc75d4c14970c0ec2e28 to your computer and use it in GitHub Desktop.
EP 14 Prefer Exceptions to Returning `None` ref: http://qiita.com/giwa/items/5ba9386970bcbccd0b80
def divide(a, b):
try:
return a/b
except ZeroDivisionError:
return None
result = divide(x, y)
if result is None:
print('Invalid inputs')
x, y = 0, 5
result = divide(x, y)
if not result:
print('Invalid inputs')
def divide(a, b):
try:
return True, a/b
except ZeroDivisionError:
return False, None
def divide(a, b):
try:
return a/b
except ZeroDivisionError as e:
raise ValueError('Invalid inputs') from e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment