Skip to content

Instantly share code, notes, and snippets.

@jhidding
Last active March 6, 2018 10:11
Show Gist options
  • Save jhidding/f5204f95896479f6e76d13f1a3c486ac to your computer and use it in GitHub Desktop.
Save jhidding/f5204f95896479f6e76d13f1a3c486ac to your computer and use it in GitHub Desktop.
expecting-the-unexpected
def something_dangerous(x):
print("computing reciprocal of", x)
return 1 / x
try:
for x in [2, 1, 0, -1]:
print("1/{} = {}".format(x, something_dangerous(x)))
except ArithmeticError as error:
print("Something went terribly wrong:", error)
# output:
# computing reciprocal of 2
# 1/2 = 0.5
# computing reciprocal of 1
# 1/1 = 1.0
# computing reciprocal of 0
# Something went terribly wrong: division by zero
input_list = [2, 1, 0, -1]
reciprocals = [something_dangerous(item)
for item in input_list]
print("The reciprocal of", input_list, "is", reciprocals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment