Chapter 2-Exception Handling (1)
Chapter 2-Exception Handling (1)
Types of Error:
(ii) Run-time errors . The errors that occur during runtime because of unexpected
situations. Such errors are handled through exception handling routines of Python.
Exceptions are unexpected events or errors that occur during the execution of a
program, such as a division by zero or accessing an invalid memory location. These
events can lead to program termination or incorrect results.
try:
# Some Code....which may have runtime error
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Example:
try:
k = 9//0 # raises divide by zero exception.
print(k)
else:
print(“No error”)
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')