Exception Handling in Python
Try-Except:-
Try block has the main block of the code it will be executed if no error occurs.
Except block will be executed if error occur in the code.
try:
#block of code
except:
#block of code
Try- Except-Else:-
Try block has the main block of the code it will be executed if no error occurs.
Except block will be executed if error occur in the code.
Else block will be executed if there are no errors.(means only when try is executed, if except gets executed the else will not be executed).
try:
#block of code
except:
#block of code
else:
#block of code
Try-Except-Else-Finally:-
Try block has the main block of the code it will be executed if no error occurs.
Except block will be executed if error occur in the code.
Else block will be executed if there are no errors.(means only when try is executed, if except gets executed the else will not be executed).
Finally will always be executed independent of all the other blocks.
try:
#block of code
except:
#block of code
else:
#block of code
finally:
#block of code
0 Comments