Exception handling3 (1) (1)
Exception handling3 (1) (1)
syllabus
2024-25
Chapter 3
Exception handling
Exception Handling
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
try:
k = 9//0 # raises divide by zero exception.
print(k)
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Advantages of Exception Handling:
• Improved program reliability
• Simplified error handling
• Cleaner code
• Easier debugging