Errors and Exceptions in Python

No matter how good you make your code, things can go wrong.

When we plan our program, we always work for situations we normally expect.

And our program works quite well in those situations.

But we often ignore the possibility of unexpected situations which can mess with the normal execution of our program.

Let’s learn about such situations and what exactly happens when our program encounters Python error and exception.


Python errors and exceptions

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

Python Error

You might have come across syntax errors millions of times in your initial stages of learning Python.

Everybody does.

And even if your syntax is correct, you might encounter errors while execution of your program.

Errors are unusual and unexpected situations that affect the normal flow of program execution.

We make errors all the time.

Some of these errors are “compile-time” errors.

These errors occur before the program execution begins, i.e., at the time of compilation.

While the other errors are “run-time” errors and occur after the code is successfully compiled and is running.

The two distinguishable types of errors in Python are:

  • Syntax errors – Compile-time error
  • Exceptions – Run-time errors

Let’s have a look at Syntax errors before talking about the ‘elephant in the room’– Exceptions.

Python Syntax Error

A syntax error occurs when you don’t follow proper rules and structure of the language.

A very common mistake that we make while getting accustomed to Python’s syntax is not using correct indentation.

This results in a syntax error.

Another example would be not putting a colon after a function header, or not enclosing a string within quotes.

>>> l = [1, 2, 3]

>>> for i in l
SyntaxError: invalid syntax

Recognize the error here?

We forgot to put a colon after “for i in l”.

And as we press Enter to start the for-loop’s block, we get a SyntaxError message.

There are so many places in your code where you can possibly go wrong.

And as soon as the compiler recognizes your mistake, it pops up a syntax error message.

Your program will not start executing until you correct all the syntax errors in your code.

Python Exceptions

Even if you get the syntax right, your program might get stuck somewhere during execution.

This is due to the errors in your code which occur at run-time.

There is no way for your compiler to recognize such errors.

Exceptions in python are the errors that occur during execution.

Well simply put, exceptions occur when your program tries to perform an operation it wasn’t supposed to.

This is when your program crashes and the interpreter displays a runtime exception message on the screen.

There are many different circumstances under which your Python program might crash.

In fact, Python’s interpreter comes up with a complete list of built-in exception types.

Common Python Exceptions

You’ll find some of the common exceptions in the table below.

ExceptionWhy it gets raised
AssertionErrorWhen an assert statement fails.
AttributeErrorWhen an attribute reference or assignment fails.
EOFErrorWhen input() function encounters End Of File(EOF).
FloatingPointErrorWhen an operation on floating-point fails.
ImportErrorWhen a module you want to import is not found.
IndexErrorWhen you try to access the element at an index which is out of range.
KeyErrorWhen a key is not present in a dictionary.
KeyboardInterruptWhen the user presses the interrupt key(Control-c or Delete).
MemoryErrorWhen an operation runs out of memory.
NameErrorWhen a variable is not found in scope.
NotImplementedErrorWhen an abstract method is not implemented anywhere in the code.
OSErrorWhen a system-related error occurs.
OverflowErrorWhen an arithmetic operation produces a really large result
RuntimeErrorWhen a run-time error doesn’t fall in any of the other categories.
StopIterationThe next() function raises it when there are no more elements in the iterable.
SyntaxErrorWhen there is an error in the syntax.
IndentationErrorWhen the indentation is not correct.
TabErrorInconsistent tabs and spaces in the indentation. It is a subclass of IndentationError.
SystemErrorWhen the interpreter detects an internal error.
TypeErrorWhen an operation or function tries to operate on an object of the incorrect type.
UnboundLocalErrorWhen you reference a local variable before assigning it a value.
ValueErrorWhen you pass an object of correct type but the improper value to a function.
ZeroDivisionErrorWhen the second operand of a division or modulo operator is zero.


In this Python error article by TechVidan, we saw what Python error is and how it can affect the normal flow of execution of our program.

We also learned about Python exceptions and their types.

A good program should be able to handle any exception that may possibly occur.

We’ll learn how to handle these exceptions in another article.

Till then, keep coding and be careful of those errors!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.