Error Handling

By now, you may have come across various errors in your Python programs. No worry, everyone makes mistakes the first time around. In this lesson, we will cover ways to handle and except errors in our Python programs.

Error Examples

There are many different error types in Python, each relating to a specific problem that could occur. Some of the most common errors made are:

  • IndexError - Caused by trying to access an element not in range
  • NameError - Caused by an undefined variable or function
  • SyntaxError - Caused by an error in syntax
  • TypeError - Caused when an operation is performed on different types

Lets take a deeper look at the last one, as it is a very important one.

TypeError

Let’s say we have a function that takes in two numbers and adds them together. Let’s call it add().

def add(x, y):
    return x + y

two_plus_two = add(2, 2) 
print(two_plus_two) # Prints 4

The add function works as expected. However, we do not check to see if the two inputs are both numbers, thus this could lead to a situation like this:

def add(x, y):
    return x + y

var1 = add(2, "Hello, World!") 

If you were to run the code above, you would be met with an error that looks like this:

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

Python tells us that we cannot add a string to an integer. Though this might seem an easy fix, it demonstrates a use-case for the following topic.

Try Except statements

Try except statements follow similar syntax to that of if/else statements. A try block will exit if an error is raised.

try:
    # Do something
except:
    # Do something if error happens

We can also specify the error in the except block by adding the name of the error we are expecting.

try:
    # Do stuff
except NameError:
    print("Something is not defined")
except TypeError:
    print("You can't do that with these two items")

Finally

You can have as many exceptions as you want. We can also have a closing block of code that will run even if the program encounters an error. This is known as the finally block.

try:
    # Do stuff
except:
    # Handle error
finally:
    # Do something after error

With this, we can go back to our example and try to handle the error we occurred.

Let’s modify the body of our function to use a try/except statement.

def add(x, y):
    try:
        return x + y
    except TypeError:
        return "Parameters of the function are of different type"
    except:
        return "Something went wrong, try again"

error = add(5, "Hello, World")

print(error) 
# Prints Parameters of the function are of different type