# Python Errors and Exceptions

You must have got some errors on running your programs when you made some mistake in the program. It is often frustrating to get errors especially when we can’t figure out the cause of that error.

Whenever we write something wrong in our program, the interpreter throws an error when we run the program. For example, if we forget to give proper indentation to the body of a loop, then on running the program, the execution gets terminated and an error named IndentationError is thrown.

In this chapter, we will look at the types of errors we can get in Python. Errors are broadly classified into two types.

1. Syntax Errors
2. Exceptions (Logical Errors)

## Python Syntax Errors

We get syntax errors when we don’t write the correct syntax of Python language.

For example, we can get a syntax error when we forget to write a colon : after the condition of the if statement as shown below.

if 2 < 3
print(2)

File "script.py", line 1
if 2 < 3
^
SyntaxError: invalid syntax

Here the error message states that the error is in line number 1 of your program. On checking the syntax of the statement written in line number 1, you can identify you missed a colon.

Look at another example.

print"Sam")

File "script.py", line 1
print"Sam")
^
SyntaxError: invalid syntax

We got a syntax error here because we didn’t put the opening parentheses bracket ( before “Sam” in line number 1 of the program.

You can see that we get syntax errors when we write some incorrect syntax. In these examples, our logic was correct but the syntax was wrong and so we got the syntax errors.

## Python Exceptions (Logical Errors)

Exceptions are logical errors which we get when the syntax is correct and there is something wrong with our logic. Or we can say that, apart from SyntaxError, all other errors are called Exceptions.

Let’s look at some common Exceptions.

### Python NameError

It is raised when a variable is not defined.

print(num)

Traceback (most recent call last):
File "script.py", line 1, in <module>
print(num)
NameError: name 'num' is not defined

We got NameError because the variable num is not defined.

### Python IndexError

It is raised when the index of a sequence (like list, tuple, etc) does not exist, or more technically speaking, it is raised when the index is out of range.

mylist = [1, 2, 3, 4]
print(mylist[10])

Traceback (most recent call last):
File "script.py", line 2, in <module>
print(mylist[10])
IndexError: list index out of range

We got IndexError on printing mylist[10] because there is no index 10 of the list mylist.

### Python KeyError

It is raised when the key of a dictionary is not found.

mydict = {1: 'a', 2: 'b', 3: 'c'}
print(mydict[4])

Traceback (most recent call last):
File "script.py", line 2, in <module>
print(mydict[4])
KeyError: 4

We got KeyError on printing mydict[4] because the dictionary mydict has no key as 4.

### Python IndentationError

It is raised when indentation is incorrect.

if 4 < 3:
print("4 is greater than 3")

File "script.py", line 2
print("4 is greater than 3")
^
IndentationError: expected an indented block

We got IndentationError because we didn’t give indentation to the body of the if statement. Atleast one statement should have indentation after the condition of if is checked to mark the body of if.

### Python ZeroDivisionError

It is raised when some number is divided by zero.

a = 0
b = 10/a

Traceback (most recent call last):
File "script.py", line 2, in <module>
b = 10/a
ZeroDivisionError: division by zero

We got ZeroDivisionError because we divided a number 10 by 0.

### Python ValueError

It is raised when the type of the argument passed to a function is incorrect.

a = int("Sam")

Traceback (most recent call last):
File "script.py", line 1, in <module>
a = int("Sam")
ValueError: invalid literal for int() with base 10: 'Sam'

We know that the int() function converts the value passed to it to an integer, and it can’t convert a string to an integer. Therefore, when we tried to convert the string “Sam” to integer by passing it to this function , we got ValueError.

We don’t need to remember these exceptions, but it’s always good to have a rough idea of the types of error or exceptions while debugging. To read about other built-in exceptions in Python, go to Python Built-in Exceptions.

Apart from these built-in exceptions, we can also create our own exceptions and can also handle exceptions about which we will learn in the next chapters.

To learn from simple videos, you can always look at our Python video course on CodesDope Pro. It has over 500 practice questions and over 20 projects.
One is not born a genius, one becomes a genius.
- Simone de Beauvoir

Recent Posts
asds
jasd
as