Error Types
Python - Error Types
Section titled “Python - Error Types”The most common reason of an error in a Python program is when a certain statement is not in accordance with the prescribed usage. Such an error is called a syntax error. The Python interpreter immediately reports it, usually along with the reason.
>>> print "hello"SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?>>> print "hello" SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello")?
In Python 3.x, print is a built-in function and requires parentheses. The statement above violates this usage and hence syntax error is displayed.
Many times though, a program results in an error after it is run even if it doesn’t have any syntax error. Such an error is a runtime error, called an exception. A number of built-in exceptions are defined in the Python library. Let’s see some common error types.
The following table lists important built-in exceptions in Python.
IndexError
Section titled “IndexError”The IndexError is thrown when trying to access an item at an invalid index.
IndexError
>>> L1=[1,2,3]>>> L1[3]Traceback (most recent call last):File "<pyshell#18>", line 1, in <module>
L1[3]IndexError: list index out of range`>>> L1=[1,2,3]
L1[3] Traceback (most recent call last): File “<pyshell#18>”, line 1, in
L1[3] IndexError: list index out of range`
ModuleNotFoundError
Section titled “ModuleNotFoundError”The ModuleNotFoundError is thrown when a module could not be found.
ModuleNotFoundError
>>> import notamoduleTraceback (most recent call last):File "<pyshell#10>", line 1, in <module>
import notamoduleModuleNotFoundError: No module named 'notamodule'`>>> import notamodule
Traceback (most recent call last):
File “<pyshell#10>”, line 1, in
import notamodule ModuleNotFoundError: No module named ‘notamodule’`
KeyError
Section titled “KeyError”The KeyError is thrown when a key is not found.
KeyError
>>> D1={'1':"aa", '2':"bb", '3':"cc"}>>> D1['4']Traceback (most recent call last):File "<pyshell#15>", line 1, in <module>
D1['4']KeyError: '4'`>>> D1={‘1’:“aa”, ‘2’:“bb”, ‘3’:“cc”}
D1[‘4’] Traceback (most recent call last): File “<pyshell#15>”, line 1, in
D1[‘4’] KeyError: ‘4’`
ImportError
Section titled “ImportError”The ImportError is thrown when a specified function can not be found.
ImportError
>>> from math import cubeTraceback (most recent call last):File "<pyshell#16>", line 1, in <module>
from math import cubeImportError: cannot import name 'cube'`>>> from math import cube
Traceback (most recent call last):
File “<pyshell#16>”, line 1, in
from math import cube ImportError: cannot import name ‘cube’`
StopIteration
Section titled “StopIteration”The StopIteration is thrown when the next() function goes beyond the iterator items.
StopIteration``next()
>>> it=iter([1,2,3])>>> next(it)1>>> next(it)2>>> next(it)3>>> next(it)Traceback (most recent call last):File "<pyshell#23>", line 1, in <module>
next(it)StopIteration`>>> it=iter([1,2,3])
next(it) 1 next(it) 2 next(it) 3 next(it) Traceback (most recent call last): File “<pyshell#23>”, line 1, in
next(it) StopIteration`
TypeError
Section titled “TypeError”The TypeError is thrown when an operation or function is applied to an object of an inappropriate type.
TypeError
>>> '2'+2Traceback (most recent call last):File "<pyshell#23>", line 1, in <module>
'2'+2TypeError: must be str, not int`>>> ‘2’+2
Traceback (most recent call last):
File “<pyshell#23>”, line 1, in
‘2’+2 TypeError: must be str, not int`
ValueError
Section titled “ValueError”The ValueError is thrown when a function’s argument is of an inappropriate type.
ValueError
>>> int('xyz')Traceback (most recent call last):File "<pyshell#14>", line 1, in <module>
int('xyz')ValueError: invalid literal for int() with base 10: 'xyz'`>>> int(‘xyz’)
Traceback (most recent call last):
File “<pyshell#14>”, line 1, in
int(‘xyz’) ValueError: invalid literal for int() with base 10: ‘xyz’`
NameError
Section titled “NameError”The NameError is thrown when an object could not be found.
NameError
>>> ageTraceback (most recent call last):File "<pyshell#6>", line 1, in <module>
ageNameError: name 'age' is not defined`>>> age
Traceback (most recent call last):
File “<pyshell#6>”, line 1, in
age NameError: name ‘age’ is not defined`
ZeroDivisionError
Section titled “ZeroDivisionError”The ZeroDivisionError is thrown when the second operator in the division is zero.
ZeroDivisionError
>>> x=100/0Traceback (most recent call last):File "<pyshell#8>", line 1, in <module>
x=100/0ZeroDivisionError: division by zero`>>> x=100/0
Traceback (most recent call last):
File “<pyshell#8>”, line 1, in
x=100/0 ZeroDivisionError: division by zero`
KeyboardInterrupt
Section titled “KeyboardInterrupt”The KeyboardInterrupt is thrown when the user hits the interrupt key (normally Control-C) during the execution of the program.
KeyboardInterrupt
>>> name=input('enter your name')enter your name^cTraceback (most recent call last):File "<pyshell#9>", line 1, in <module>
name=input('enter your name')KeyboardInterrupt`>>> name=input(‘enter your name’)
enter your name^c
Traceback (most recent call last):
File “<pyshell#9>”, line 1, in
name=input(‘enter your name’) KeyboardInterrupt` Learn how to handle exceptions in Python in the next chapter.