InterviewSolution
Saved Bookmarks
| 1. |
Exception Handling in Python |
|
Answer» Exception Handling is used to handle situations in our program flow, which can inevitably crash our program and hamper its normal working. It is done in Python using try-except-finally keywords.
The same has been illustrated in the image below: Example: # divide(4, 2) will return 2 and print Division Complete# divide(4, 0) will print error and Division Complete # Finally block will be executed in all cases def divide(a, denominator): try: return a / denominator except ZeroDivisionError as e: print('Divide By Zero!! Terminate!!') finally: print('Division Complete.') |
|