Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created April 9, 2020 14:42
Show Gist options
  • Save azamsharp/3e4869d8fc0dad8ab1a4207527d4908b to your computer and use it in GitHub Desktop.
Save azamsharp/3e4869d8fc0dad8ab1a4207527d4908b to your computer and use it in GitHub Desktop.
# Exceptions
def ask_user_for_input():
print("asking user for input")
number = int(input("Enter number: "))
try: # capturing runtime errors
#ask_user_for_input()
#result = 1/0
result = 4/2
# number = int(input("Enter number: ")) # ValueError
#with open("guests.txt") as file:
# file.read()
except ZeroDivisionError:
print("Hey don't divide by zero")
except ValueError:
print("Please only enter numbers..")
except FileNotFoundError:
print("File does not exists!")
except:
print("Opps something happened!")
else: #fired when no exception has happened..
print("Everything is good!")
finally: # fired when exception happens and also fired when exception does not happen
# clean up resource
# database connections/ socket connections/ file io
print("FINALLY BLOCK FIRED")
print("hey what about me!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment