try:
# Code to test / execute
print('Test')
except (SyntaxError, IndexError) as E: # specific exceptions
# Code in case of SyntaxError for example
print('Synthax or index error !')
except:
# Code for any other exception
print('Other error !')
else:
# Code if no exception caught
print('No error')
finally:
# Code executed after try block (success) or any exception (ie everytime)
print('Done')
# This code is out of try / catch bloc
print('Anything else')
try:
a=2*3
except TypeError:
print("Exception raised")
else:
print("Everything is ok.")
# program to print the reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)