Introduction to Error handling

Error Handling

When we work with Python, especially during the phase where we test our code, we are sure to face an error or more. Using Error Handling we have a way to modify the way Python should respond if there is an error, this is exponentially useful as the default method of responding to an error is that Python closes the program.

Let’s look at an example:

x = 10

if x > 7:
print("x is greater than 7")
elif x <. 7:
print("x is less than 7")
else:
print("x is equal to 7")

Here we have a code snippet that has an error, the ‘elif’ statement has a ‘.’ after the ‘<’ symbol. When we run this code, we should get the following error message:

It raises a ‘SyntaxError’ and the program crashes. However, with Error Handling, we can make sure the program does not crash when an error comes up but instead handle it and conclude the program in the right way.
If there was no right way to handle a program when it faces an error, we would not be able to build programs of the scale that we have today. An efficient and excellent program can automatically handle errors without human intervention.

Types of Errors

Before we learn how to handle an error, let us consider the different types of Errors we face in Python.

  • Syntax Error
  • Logical Error

Syntax Error

As we saw earlier, a ‘SyntaxError’ is brought up when the rules of the programming languages are violated. Let’s consider a few examples of these errors.

1. Missing Colon

for x in range(3, 10)
  print(x)

Here we have missed a colon at the end of the for loop, and so Python will inform us:

2. Missing double quotes

user_input = input("Enter your value)
print(user_input)

Here we have missed the end of the double quotation mark at the end of the input function, and we will be informed about it:

3. Missing symbol

x = 15

if x = 15:
print(x)

Here we have missed the second ‘=’ symbol whereas for comparison we need a double ‘=’ symbol, and Python tells us about this error:

Logical Error

These are errors that are raised when Python is executing our lines of code. Examples of Runtime Errors are when we mistype a variable name or try to call a function or variable that does not exist or try to access the index of an item that does not exist in the list. We also refer to these types of errors as an Exception.

Consider the following scenarios:

    1. Mistyping Function Name

device = "Laptop"

def laptop_device():
print("Yay, I got a", device)

Laptop_Device()

We will get a ‘NameError’ asshown below:

 2. Incorrect Index

items = [3, 5, 7, 9, 11]

# items list has only 5 items and the index ends at 4
print(items[7])

Here we are trying to access a value that is not stored in the items list. We get the following ‘IndexError’:

 

     3. Non-Existing Key in Dictionary

dict_list_ = {
  "item_1": "First Item",
  "item_2": "Second Item",
  "item_3": "Third Item"
}

print(dict_list_["item_1"])
print(dict_list_["item_2"])
print(dict_list_["item_3"])
print(dict_list_["item_4"])

We are trying to access an item that does not exist in a dictionary. Thus, Python will result in a ‘KeyError’:

A list of the some of the common types of Errors we can face include:

Serial Number Exception Description
1. AttributeError Raised when an assignment or reference of an attribute fails.
2. ImportError Raised when the imported module is not found
3. KeyboardInterrupt Raised when the user presses the interrupt key.
4. MemoryError Raised when the memory required for an operation runs out.
5. NameError Raised when a called variable is not found.
6. OSError Raised when a system operation causes a related to the system.
7. ZeroDivisionError Raised when we try to divide a number by 0.

In the upcoming blogs, we will consider the different ways we can handle these errors.

What have we learned?

  • What is Error Handling?
  • How many types of Errors do we have in Python?
  • What are some of the situations that lead to syntax errors?
  • What are some of the situations that lead to logical errors?
  • What is an Exception?
  • What are some of the most common errors?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments