16.1. Exception About

  • Used when error occurs

  • You can catch exception and handles erroneous situation

  • If file does not exists

  • If no permissions to read file

  • If function argument is invalid type (ie. int('one'))

  • If value is incorrect (ie. negative Kelvin temperature)

  • If network or database connection could not be established

16.1.1. NameError

  • Local or global name is not found

>>> print(firstname)
Traceback (most recent call last):
NameError: name 'firstname' is not defined

16.1.2. IndexError

  • Sequence subscript is out of range

>>> colors = ['red', 'green', 'blue']
>>>
>>> colors[10]
Traceback (most recent call last):
IndexError: list index out of range

16.1.3. KeyError

  • Dictionary key is not found

>>> user = {
...     'firstname': 'Alice',
...     'lastname': 'Apricot',
... }
>>>
>>> user['email']
Traceback (most recent call last):
KeyError: 'email'

16.1.4. SyntaxError

  • Parser encounters a syntax error

>>> if True
...     print('hello')
...
Traceback (most recent call last):
SyntaxError: expected ':'

16.1.5. IndentationError

  • Syntax errors related to incorrect indentation

>>> if True:
...     print('one')
...      print('two')
...     print('three')
...
Traceback (most recent call last):
IndentationError: unexpected indent

16.1.6. ValueError

  • Argument has an invalid value

>>> float('1')
1.0
>>> float('one')
Traceback (most recent call last):
ValueError: could not convert string to float: 'one'

16.1.7. TypeError

  • Operation or function is applied to an object of inappropriate type

>>> colors = ['red', 'green', 'blue']
>>>
>>> float(colors)
Traceback (most recent call last):
TypeError: float() argument must be a string or a real number, not 'list'

16.1.8. AttributeError

  • Attribute reference or assignment fails

>>> colors = ('red', 'green', 'blue')
>>>
>>> colors.append('black')
Traceback (most recent call last):
AttributeError: 'tuple' object has no attribute 'append'

16.1.9. IsADirectoryError

  • Trying to open directory instead of file

>>> open('/tmp')
Traceback (most recent call last):
IsADirectoryError: [Errno 21] Is a directory: '/tmp'

16.1.10. FileNotFoundError

  • File does not exists

>>> open('myfile.txt')
Traceback (most recent call last):
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'

16.1.11. ModuleNotFoundError

  • Module could not be located

>>> import nasa
Traceback (most recent call last):
ModuleNotFoundError: No module named 'nasa'

Note, that this exception is also raised when you don't have this module installed. Such as while importing pandas or numpy without installing it first.

16.1.12. ZeroDivisionError

  • Division or modulo by zero

>>> 1 / 0
Traceback (most recent call last):
ZeroDivisionError: division by zero
>>> data = []
>>>
>>> mean = sum(data) / len(data)
Traceback (most recent call last):
ZeroDivisionError: division by zero

16.1.13. KeyboardInterrupt

  • User interrupts program execution

  • Usually by pressing Ctrl+C

  • PyCharm: Stop button in the Python Console toolbar

While running the following code, you can interrupt it by pressing Ctrl+C:

>>>
... while True:
...     pass
...
Traceback (most recent call last):
KeyboardInterrupt