Python Exception or Error During the Run Time

By | April 12, 2021
Python Exception or Error During the Run Time

In this tutorial, we will discuss what are python exception and when and why they occur. There are many kinds of error in python and one of the most common error is syntax error this error occurs due to some misalignment of syntax and the python interpreter could not understand what that syntax wants to say.

Python Exception


for i in range()


SyntaxError: invalid syntax

Behind the code:

In the above example we got an error because we did not put a colon(:) after the range() so the interpreter get confused with the statement and throw an error.


Sometimes we see that our python code gets executed and the interpreter does not collect any error from the code and suppose in that code we ask a user to enter an integer value, but the user enters a string instead, this could cause an error this kind of error known as Exception.

An exception can be defined as those errors occur in the program during the runtime or by some unexpected user input.


Some most common Exception are FileNotFoundError, ZeroDivisionError, ValueError and ImportError.


integer = int(input("Please enter an Integer value:"))


Please enter an Integer value: techgeekbuzz

ValueError: invalid literal for int() with base 10: 'techgeekbuzz'

Behind the code:

In the above code, the programme asked for an integer value but we provided a string, this causes a runtime error.

Python Built-In exception:

There are many built-in Exception in python that can raise an error. We can see all the built-in exception using the locals() built-in function



Built-in Python Exception:

Exceptions Details
AssertionError Due to failure of assert statement
AttributeError Occur due to an illegal number of attribute pass
EOFError Due to the input() function
FloatingPointError Due to floating point
GeneratorExit Due to generator’s close() method.
ImportError When there is no such import module
IndexError Out of range index called
KeyError Unknown key passed along a dictionary.
KeyboardInterrupt When user enter ctrl+c
MemoryError Due to out of memory
NameError When such a variable is not defined.
NotImplementedError Due to the abstract method
OSError System-related error.
OverflowError When an arithmetic value is out of range
ReferenceError Due to weak reference proxy
RuntimeError Error during runtime
StopIteration When there is no element left and we call next()
SyntaxError Due to illegal syntax
IndentationError Due to illegal indentation
TabError Due to illegal indentation
SystemError Internal error
SystemExit Due to exit programme statement sys.exit()
TypeError When two different data type print together
UnboundLocalError When there is no such local variable.
UnicodeError Encoding and decoding error
ValueError Due to incorrect value pass in function argument
ZeroDivisionError When any number divided by 0
Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

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