Python try...except
Python try...except
try….. except blocks are used in python to handle errors and exceptions. The code in try block runs when there is no error. If the try block catches the error, then the except block is executed.
Example:
Output 1:
Output 2:
Output 3:
In addition to try and except blocks, we also have else and finally blocks.
The code in else block is executed when there is no error in try block.
Example:
Output 1:
Output 2:
The finally block is execute whatever is the outcome of try……except…..else blocks.
Example:
Output 1:
Output 2: