Skip to content

Instantly share code, notes, and snippets.

@nagos
Last active June 8, 2023 10:39
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 nagos/39172d52f7515eb9cd435a609db48a12 to your computer and use it in GitHub Desktop.
Save nagos/39172d52f7515eb9cd435a609db48a12 to your computer and use it in GitHub Desktop.
def calc(x):
print("-- Calculating")
try:
result = 5 / x
except ZeroDivisionError:
print("-- Expected error, continue")
result = 0
print("-- Result ready")
return result
if __name__ == '__main__':
try:
print("Run 1")
res1 = calc(2)
print(f"Result {res1}")
except:
print("Fatal error")
print()
try:
print("Run 2")
res2 = calc(0)
print(f"Result {res2}")
except:
print("Fatal error")
print()
try:
print("Run 3")
res3 = calc("test")
print(f"Result {res3}")
except Exception as err:
print(f"Fatal error {err}")
@nagos
Copy link
Author

nagos commented Jun 8, 2023

Output:

Run 1
-- Calculating
-- Result ready
Result 2.5

Run 2
-- Calculating
-- Expected error, continue
-- Result ready
Result 0

Run 3
-- Calculating
Fatal error unsupported operand type(s) for /: 'int' and 'str'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment