Errors and exception handling in Python

A guide to learning about errors and exception handling in Python

Jayashree domala
Level Up Coding

--

Photo by mostafa meraji on Unsplash

When writing code, errors are bound to happen but error handling can be done to prepare for these errors. Normally when there is an error, the script stops working. But error handling can be used to let the script continue even if there is an error.

There are 3 keywords we need to know about for this purpose. They are as follows:

1)try

This is the block of code to be run and may lead to an error

2) except:

This block of code will be executed in case there is an error raised in the try block.

3) finally:

This block of code is to be executed regardless of an error.

So let's assume a function "summation" which adds two numbers. This function runs properly.

>>> def summation(num1,num2):
print(num1+num2)
>>> summation(2,3)
5

Now let's take one input from the user and run the function.

>>> num1 = 2
>>> num2 = input("Enter number: ")
Enter number: 3
>>> summation(num1,num2)>>> print("This line will not be printed because of the error")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-2cc0289b921e> in <module>
----> 1 summation(num1,num2)
2 print("This line will not be printed because of the error")

<ipython-input-1-970d26ae8592> in summation(num1, num2)
1 def summation(num1,num2):
----> 2 print(num1+num2)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

So we get an error "TypeError" because we are trying to add a number and a string. And notice that after the error has occurred, the lines of code after that won't be executed. So we use the keywords mentioned above to make sure that even if an error occurs, the script runs.

>>> try:
summed = 2 + '3'
except:
print("Summation is not of the same type")
Summation is not of the same type

So here we can see that when there is an error in try block, it goes to except and prints the statement. Now let's add 'else' block in case no error is encountered.

>>> try:
summed = 2 + 3
except:
print("Summation is not of the same type")
else:
print("There was no error and result is: ",summed)
There was no error and result is: 5

Now let's try understanding this with another example wherein the except block we mention the error also. If you don't mention the error type in except block, it will assume it for all exceptions.

>>> try:
f = open('test','w')
f.write("This is a test file")
except TypeError:
print("There is a type error")
except OSError:
print("There is an OS error")
finally:
print("This will print even if no error")
This will print even if no error

Now creating an error to see if the except block works along with the finally block

>>> try:
f = open('test','r')
f.write("This is a test file")
except TypeError:
print("There is a type error")
except OSError:
print("There is an OS error")
finally:
print("This will print even if no error")
There is an OS error
This will print even if no error

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

--

--

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.