Catching exceptions can help with errors in that whenever a runtime error occurs, it creates an exception object. This program stops running and python prints out the traceback, which ends with a message describing the exception that occurred.
#example one
try:
N = 42
N = N+ ' 4 '
except TypeError:
print('Wrong type')
#example two
try:
m = []
print(m[1])
except IndexError:
print("Index error")
#Example three dividision by zero
try:
y=int(input("Enter an interger: "))
x = y/0
print(x)
except ZeroDivisionError:
print(Zero)
Comments
Leave a comment