# Simple exceptions
''' Here are a few exceptions, try executing one while others are commented'''
print (5/0) # ZeroDivisionError
print ("string" + 343) # TypeError
file = open ("./test.txt") # FileNotFoundError
# Simple try-catch to avoid Exceptions
try:
print (5/0)
except ZeroDivisionError:
print ("Cannot Divide by zero")
# The else block
# If the try block executes successfully, then the else block is executed
deno = 1
try:
print (5/deno)
except ZeroDivisionError:
print ("Deno is zero.")
else:
print ("Deno is not Zero.")
# Handle expection and not do anything
deno = 0
try:
print (5/deno)
except ZeroDivisionError:
pass
else:
print ("Deno not Zero.")
# Catch expection without explicitly knowing the type of exception
deno = 1
try:
''' Here are a few exceptions, try executing one while others are commented'''
print (5/deno)
print ("string" + 343)
file = open ("./test.txt")
except:
print ("Some exception!")
else:
print ("The try somehow executed completely!\nDid you comment all lines?")