Exception Handling in Python

 Exception Handling in Python

Exception is a unusual condition in the code. If error occurs the execution would be stopped so to avoid interruption exception handling is used. 

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




Post a Comment

0 Comments