Error Handling

You're currently learning a lecture from the course: 


Prerequisite Terminologies

In order to have thorough understanding of the main topic, you should have the basic concept of the following terms:

Errors & its types




Muneeza Maqsood


Error handling refers to the response and recovery procedures from error conditions that can be occurred while executing a program. Error handling is provided built-in within the python that can be utilized through certain keywords. It is an efficient way to improve your Python code that you’re trying to work with. When you think that you have a code that can cause an error you can utilize error handling to avoid the error causing code to crash your program.

  • Keywords for Error Handling:

Error handling is utilized within python through four keywords. Three of the keywords are dedicated to error handling and one to if-else.

1st.  Keywords

2nd. How they work


Code block 1

#....Run this code


Code block 2

#....Execute this code when there is an error


Code block 3

#....No error? Run this code


Code block 4

#....Always run this code


  • We’ll first go through with the error handling keywords then utilize them to handle errors within python code.


code block 1

#. . .some error prone code. . .


code block 2

#. . .do something with the error. . .


code block 3

#. . .to do when there is no error. . .


code block 4

#. . .some clean up code. . .

  • try:

This segment will try to run the code and see if there is any error within your code. For example our code block 1 is causing an error, in that case the code that causes error will execute ‘except’ block of code.

  • except:

When an error occurs in the ‘try’ block of code, then this block of code will run that says, you can do anything with the error you’ve received.

  • else:

If there is no error in the ‘try’ code block, then any code in the ‘else’ block will run.

  • finally:

Then code block 4 will run which is not related to the other three keywords. ‘finally’ block always executed after leaving the ‘try’ statement.

Possible Errors:

  • In any programming language, there are possible scenarios in which errors can occur while writing the code. For example, you’ve written code that reads a file and outposts the data available in the file. In case when you’re doing it for personal use, you just provide the file name, try to read the file, open it and print out its results through python code, this would work fine. But when you share the file or you’re trying to read other files, in this case you’ve to change the file and the directory.

  • Error depends on how you write the code. So you should be efficient with writing the code or able to handle errors quite efficiently and provide the error in a human readable form.

  • Create your file in PyCharm as .py and get working directory.

              Import os


       [It’ll print out the working directory]

  • Open and read a particular file (that is not present in the working directory), providing filename and read note.

              file = open(“filename” . “r”)


       [It’ll give an error as “FileNotFoundError” & No such file or directory: “filename that

you provided”]

       [So here we get an error while running the code because it is not a professional way to read a particular file as files can change or you can get new code within your directory over time.]

  • Utilizing Error Handling Keywords:

  • We’ll try to handle error within the same code utilizing the keywords discussed earlier.


              file = open(“filename” , “r”)


              print(“We didn’t find the file you’re trying to work with.”)

              print(“Please create a file named ‘filename’.”)


              data =


       [So here how the above code will work]


‘Try to open and read the file’

As no such file, it’ll enter the ‘except’ block.


Show you error in human readable form as:

“We didn’t find the file you’re trying to work with” and “Please create a file named ‘filename’.”


Due to an error, it’ll not enter the ‘else’ block.

  • Now create a file that you were trying to open and read before and store any data in the file and run the following code:

              Import os



              file = open(“filename” , “r”)


              print(“We didn’t find the file you’re trying to work with.”)

              print(“Please create a file named ‘filename’.”)


              data =


       [Here how the above code will work]


‘Try to open and read the given file’

As the file now exists in the directory so it means there is no error.


As there is no error it’ll not enter the ‘except’ block.


No error, so it’ll open and print the data in the file.


In this video, we learned how we can improve our code in Python and handle the errors in code utilizing error handling. We also got to see how error handling keywords work to handle the error

within the Python code in an efficient way.

File(s) Section

If a particular file is required for this video, and was discussed in the lecture, you can download it by clicking the button below.

Get in touch with us

Tel: +44 7518 945488


  • Black Instagram Icon
  • Facebook


4 Mann Island, Liverpool, Merseyside, United Kingdom

Qurtaba Heights, Islamabad, Pakistan

© Copyright 2020 BioCode Ltd. - All rights reserved.