Python Exception Handling
In this article, we will discuss how to handle exceptions in Python using try, except, and finally statements with the help of proper examples.
Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which change the normal flow of the program.
There are two types of error one is syntax error and another one is logical error. Syntax error comes at the time of compile and Logical error comes at the time of execution. Every error breaks the program execution. If want to handle error then we need to use exception handling.
1.Syntax Error :
As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program.
There is a syntax error in the code . The ‘if’ statement should be followed by a colon (:), and the ‘print’ statement should be indented to be inside the ‘if’ block.
Exceptions are raised when the program is syntactically correct, but the code results in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.
2. Type Error :
This error is occured when an operation or function is applied to an object of the wrong type. Here’s an example:
Here a ‘TypeError’ is raised as both the datatypes are different which are being added.
The code attempts to add an integer (‘x’) and a string (‘y’) together, which is not a valid operation, and it will raise a ‘TypeError’.
For resolve this type error then we used a ‘try’ and ‘except’ block to catch this exception and print an error message.
1. Try and Except Statement :
Try and except statements are used to catch and handle exceptions in Python. Try block will get executed everytime. If there is any error found in try block it will execute except block. If there no error found in try block it won’t execute except block.
Syntax :
try:
#logic
except:
#logicExample :
In this above In[4], If in try block there is error occured so except block is exicuted. In another In[5], in try block there is not any error occured tha’s why try block exicuted .
Try Except & Finally Statement:
Try block will execute everytime. If there is error found in try block then execute except block .Eexcept will get executed only when there is error in try block. finally will execute everytime doesn’t matter if there is error or no error
Get Vrushali Walekar’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax :
try:
#logic
except:
#logic
finally:
#logicExample :
we can also find the error message, Let’s see the related example.
Try Except Else & finally statement:
Try block will execute everytime. If there is any error found in try block the except block will execute. If there is no error found then else block will execute and finally. Execpt will execute if there is any error occured in try block. Else block will execute if there is no error found in try block. finally block will execute everytime.
Syntax :
try:
#logic
except:
#logic
else:
#logic
finally:
#logicExample :
In the In[11] Try block not having any error so else and finally block will get exicuted. In[12] Try block having a error so except and finally block will get exicuted.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Thanks for reading My article.








