Errors and Exceptions Handling In Python
Published: 2021-01-25 23:13:45

As a developer, often times the code we write will contain some errors. So how do we set-up our errors and exceptions handling call? That’s what this tutorial is all about.

Take a glass of coffee, relax and let’s dive in!


The key to catching errors in python code is these three keywords:
**Try**, **Except** and **Finally**. We can use them to dictate our code logic in case of an error in our code.

To show how this works, we will be working with files, and one way to open the file in Python is to use the open() function.

Syntax: open keyword (name of file + extension, operation)…**Open(“name.txt”, “r”)**

The first parameter is the open keyword, follow by the actual file you want to work with and the last parameter is the operation your want to perform on the file which could be read, write and the like.

So we are going to be using this file operation to learn how errors and exceptions handling works.

To begin with, why do we actually want to search for errors in our code? Imagine we try to print **“I love Python”** using Python print function and let’s say we forget to close the second quote as shown below. That’s one type of error which is called syntax error.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hkqws5u8ctyimab5gcgr.PNG)

Let’s look at another type of error that might occur, assuming we print a list without actually define it, the programme was unable to run as showed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/umswohm1hazzmu6fmv7x.PNG)

All these name errors that pop up during our code execution allows us to easily identify what is wrong with our code and those type of error descriptions is known as an exception.


Now let’s see how to use the three exception handling keywords
**try**:
  # you perform some operation here…
**except** Exception 1:
  #if there is an exception, then execute this block..
**except** Exception 2:
  # if there is an exception, then execute this block..

**else**:
  # if there is no exception, execute this block…

Let’s see this in action. Since we are working with the file, we will create a **sample.txt** file to read and write into.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bggsdhv0t05sx8wuf5nf.PNG)

If you run the above code everything will work fine. But try to change the write operation to read and see the error.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rvloxdprt8o77ti2pgw2.PNG)

It throws some error and continue executing the code, but try to run the programme without the keywords and see what will happen.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/behf9h8mn6lx5ais8sxg.PNG)

As you can see it throws an error without ways to handle it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ec32oum6t7vty5avm7uq.PNG)

And the last keyword which is the **Finally** keyword allows whatever code that is inside it to run no matter what. The code inside the final keyword will work no matter what.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fpvuw7xd4ifo2ty9yra8.PNG)

And that’s a quick guide to working with Python errors and exceptions handling, I hope it helps. Feel free to let me know if there is anything I might have left untouched. **Thanks for reading**.