Created
March 6, 2016 13:44
-
-
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
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
def divide(a, b): | |
try: | |
return a/b | |
except ZeroDivisionError: | |
return None | |
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
result = divide(x, y) | |
if result is None: | |
print('Invalid inputs') | |
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
x, y = 0, 5 | |
result = divide(x, y) | |
if not result: | |
print('Invalid inputs') |
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
def divide(a, b): | |
try: | |
return True, a/b | |
except ZeroDivisionError: | |
return False, None | |
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
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